2016-12-04 54 views
1

我使用Firebase實時數據庫並希望在交易中爲我的矢量添加值,即獲取矢量,更新,設置矢量。當DatabaseReference :: RunTransaction出現時,是否可以向lambda添加捕獲值?

我寫了一些簡單的labda,但堅持使用該代碼不會編譯。

auto transaction = [&key](firebase::database::MutableData* data){ 
    auto value = data->GetValue(); 
    if(!value.is_vector() or value.is_null()) 
     return firebase::database::kTransactionResultAbort; 
    std::vector<firebase::Variant> taskList = value.vector(); 
    //!! taskList.push_back(firebase::Variant(key)); !!// 
    data->SetValue(firebase::Variant(taskList)); 
    return firebase::database::kTransactionResultSuccess; 
}; 
dbref.Child("https://stackoverflow.com/users/" +userId+ "/tasks/").RunTransaction(transaction); 

我想將param添加到該labda中。我能以另一種方式做到嗎?

Compiller錯誤:

tasksmanager.cpp:62:72: error: no matching function for call to 'firebase::database::DatabaseReference::RunTransaction(TasksManager::createTask(Task*)::<lambda(firebase::database::MutableData*)>&)' 
    dbref.Child("https://stackoverflow.com/users/" +userId+ "/tasks/" + key).RunTransaction(tran); 
                     ^
tasksmanager.cpp:62:72: note: candidate is: 
In file included from ..\firebase_cpp_sdk\include/firebase/database.h:9:0, 
       from tasksmanager.cpp:2: 
..\firebase_cpp_sdk\include/firebase/database/database_reference.h:178:24: note: firebase::Future<firebase::database::DataSnapshot> firebase::database::DatabaseReference::RunTransaction(firebase::database::TransactionResult (*)(firebase::database::MutableData*), bool) 
    Future<DataSnapshot> RunTransaction(DoTransaction* transaction_function, 
         ^
..\firebase_cpp_sdk\include/firebase/database/database_reference.h:178:24: note: no known conversion for argument 1 from 'TasksManager::createTask(Task*)::<lambda(firebase::database::MutableData*)>' to 'firebase::database::TransactionResult (*)(firebase::database::MutableData*)' 

回答

0

我研究了這個問題,我可以給出一個答案後; 對於該工作流程,我們可以使用C++ 11中的std::function類(或從不);

std::function<firebase::database::DoTransaction> callback; 
callback = [any captures here](firebase::database::MutableData* mutableData) { ..yours code here... }; 
databaseReference.RunTransaction(callback.target<firebase::database::DoTransaction>()); 
相關問題