0
使用此處的函數從異步塊返回值(僅數據庫查詢)。問題是由於內存問題,應用程序凍結並終止。尋求建議是否最好在主線上運行它,還是應該避免這種情況?注意它正在另一個線程上執行。異步塊返回問題
- (NSString *)databaseQuery:(NSString*)ingredient {
__block NSString *valueType = nil;
__block BOOL done = NO;
[[[_ref child:@"ingredients"] queryEqualToValue:valueType childKey:ingredient] observeSingleEventOfType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot * _Nonnull snapshot) {
for (FIRDataSnapshot *child in snapshot.children) {
valueType = child.value;
}
done = YES;
} withCancelBlock:^(NSError * _Nonnull error) {
NSLog(@"%@", error.localizedDescription);
done = YES;
}];
while (!done) {
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
}
return valueType;
}
UPDATE 1:用下面的代碼試圖並可以產生相同的結果。
- (NSString *)databaseQuery:(NSString*)ingredient {
__block NSString *valueType = nil;
dispatch_semaphore_t sem = dispatch_semaphore_create(0);
FIRDatabaseQuery *query = [[_ref child:@"ingredients"] queryEqualToValue:valueType childKey:ingredient] ;
[query observeEventType:FIRDataEventTypeChildAdded
withBlock:^(FIRDataSnapshot * _Nonnull snapshot) {
valueType = snapshot.value;
dispatch_semaphore_signal(sem);
}
withCancelBlock:^(NSError * _Nonnull error) {
NSLog(@"%@", error.localizedDescription);
dispatch_semaphore_signal(sem);
}];
dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
return valueType;
}
更新2:
改變了格式所以函數不從塊返回。它只返回FIRDatabaseQuery。
- (FIRDatabaseQuery *)databaseQuery:(NSString*)ingredient {
__block NSString *valueType = nil;
FIRDatabaseQuery *query = [[_ref child:@"ingredients"] queryEqualToValue:valueType childKey:ingredient];
return query;
}
下面的部分是在另一個過程中。除了返回的值爲空。
query = [self databaseQuery:substring];
[query observeEventType:FIRDataEventTypeChildAdded
withBlock:^(FIRDataSnapshot * _Nonnull snapshot) {
idValue = snapshot.value;
}
withCancelBlock:^(NSError * _Nonnull error) {
NSLog(@"%@", error.localizedDescription);
}];
NSLog(@"%@", idValue);
你能給出關於內存問題的更多細節嗎?哪條線/你有什麼例外? 並且您是否嘗試使用'dispatch_group_t'而不是在while循環內運行'NSRunLoop'? –
我可以嘗試使用派遣組t。內存不是問題,只是應用程序的結果被卡在線程中。 –
嘗試了另一個產生相同結果的想法。 –