在 View Controller 中我调用了这个方法:
- (void)method{
PFQuery *query = [PFQuery queryWithClassName:@"AllFiles"];
[query whereKey:@"userID" equalTo:[[NSUserDefaults standardUserDefaults] stringForKey:@"KeyMyUserID"]];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
NSMutableArray *localIds = [DBManager getAllFileIdsForUserID:[[NSUserDefaults standardUserDefaults] stringForKey:@"KeyMyUserID"]];
NSMutableArray *onlineIds = [[NSMutableArray alloc] init];
NSLog(@"log1");
for (PFObject *object in objects)
[onlineIds addObject:object[@"assignedObjectId"]];
NSLog(@"log2");
for (PFObject *object in objects)
if ([DBManager objectWasDeletedLocallyWithId:object[@"assignedObjectId"]]){
[self deleteFileForUserID:object[@"userID"] withObjectId:object[@"assignedObjectId"] completition:nil];
}else if (![localIds containsObject:object[@"assignedObjectId"]] && ![DBManager objectWasDeletedLocallyWithId:object[@"assignedObjectId"]]){
[self downloadFileWithObjectId:object[@"assignedObjectId"] forUserID:object[@"userID"]];
}
for (NSString* objectId in localIds)
if (![onlineIds containsObject:objectId])
[self uploadFileWithObjectId:objectId];
}else{
// Log details of the failure
NSLog(@"Error: %@ %@", error, [error userInfo]);
}
NSLog(@"log3");
}];
}
发生了一件非常奇怪的事情,这是我以前从未见过的。似乎方法执行在初始 for 循环后停止。
这样,输出log1,其他两个log no。如果我删除
for (PFObject *object in objects)
[onlineIds addObject:object[@"assignedObjectId"]];
然后神奇地输出了 log1 和 log2,但没有输出 log 3。 如果我这样做,它仍然不起作用:
for (PFObject *object in objects){
[onlineIds addObject:object[@"assignedObjectId"]];
}
这毫无意义。我在其他部分使用了完全相同的代码并且它有效。好像 for 循环中有一个错误。有人以前见过这个吗?
最佳答案
可能有一个监听器对该 addObject: 方法作出 react 。该监听器的执行可能会崩溃。
我在触发对 mapView:rendererForOverlay: 的调用的 MKMapView 上使用 addOverlay: 时遇到了类似的问题,并且该调用崩溃了。
卡洛斯。
关于IOS - 方法在 for 循环后停止执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27703354/