[英]Use Variable outside Firebase Block
Hello everyone I'm using Firebase for my ios project ... I have a lot of trouble using the values of my variables outside of the Firebase blocks when I query the database. 大家好,我在iOS项目中使用Firebase ...在查询数据库时,在Firebase块之外使用变量值存在很多麻烦。
For example in this case I'm trying to get a number value from this query ... 例如,在这种情况下,我正在尝试从此查询中获取数字值...
FIRDatabaseReference *userDbRef = FIRDatabase.database.reference;
[[[userDbRef child:RTDUSER] child:userID] observeSingleEventOfType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot * _Nonnull snapshot) {
NSUInteger totalCFU = [snapshot.value[RTDUSER_TOTALCFU] unsignedIntegerValue];
} withCancelBlock:nil];
I need this number value also obtained outside the block (in other functions of this class) ... 我需要在块外(在此类的其他函数中)也获得的该数字值...
How can I use the TotalCFU variable outside the firebase block? 如何在firebase块外使用TotalCFU变量?
You can call a method from inside the block to handle the value. 您可以从块内部调用方法来处理该值。
FIRDatabaseReference *userDbRef = FIRDatabase.database.reference;
[[[userDbRef child:RTDUSER] child:userID] observeSingleEventOfType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot * _Nonnull snapshot) {
NSUInteger totalCFU = [snapshot.value[RTDUSER_TOTALCFU] unsignedIntegerValue];
// Call a function to handle value
[self doSomethingWithCFU: totalCFU];
} withCancelBlock:nil];
Somewhere else in your class: 班上其他地方:
(void)doSomethingWithCFU:(NSUInteger)totalCFU {
// Do something with the value
}
Use __block
to use your variable outside. 使用
__block
在外部使用变量。
FIRDatabaseReference *userDbRef = FIRDatabase.database.reference;
__block NSUInteger totalCF;
[[[userDbRef child:RTDUSER] child:userID] observeSingleEventOfType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot * _Nonnull snapshot) {
totalCFU = [snapshot.value[RTDUSER_TOTALCFU] unsignedIntegerValue];
} withCancelBlock:nil];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.