我正在从我的解析数据库中检索数据并将其循环到NSMutableArray中。这是我的getHomes方法,它在viewDidLoad中调用。 NSMutableArray包含各种UIImages,CGSize'我正在循环。此方法称为getCellHeights。我想在getHomes方法之后立即调用此方法,但是在viewdidLoad中像这样堆叠它们并不能解决问题。 getCellHeights方法没有运行,因为NSMutableArray中没有对象,因为它没有完成检索数据。
如何在getHomes方法完成之前确保getCellHeights未运行?
-(void)getcellHeights {
for (int i = 0; i < homesDic.count; i++) {
UIImage *image = [[homesDic objectAtIndex:i] objectForKey:@"image"];
CGFloat divider = image.size.width/145;
CGFloat wantedWidth = 145;
CGFloat wantedHeight = image.size.height/divider;
NSLog(@"width: %f height: %f", wantedWidth, wantedHeight);
CGSize size = CGSizeMake(wantedWidth, wantedHeight);
[_cellSizes addObject:[NSValue valueWithCGSize:size]];
}
}
-(void)getHomes {
PFQuery *query = [PFQuery queryWithClassName:@"Homes"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects1, NSError *error) {
if (!error) {
for (PFObject *object in objects1) {
PFQuery *userQ = [PFUser query];
[userQ getObjectInBackgroundWithId:[object objectForKey:@"userId"] block:^(PFObject *userName, NSError *error) {
NSLog(@"%@", userName);
[[userName objectForKey:@"file"] getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
UIImage *profileImage = [UIImage imageWithData:data];
PFQuery *homeQ = [PFQuery queryWithClassName:@"homeImages"];
[homeQ whereKey:@"homeId" equalTo:object.objectId];
[homeQ whereKey:@"number" equalTo:@(0)];
[homeQ findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
PFQuery *cityQ = [PFQuery queryWithClassName:@"City"];
id zip = [NSNumber numberWithInteger: [[object objectForKey:@"zip"] intValue]];
[cityQ whereKey:@"fra" greaterThanOrEqualTo:zip];
[cityQ whereKey:@"fra" lessThanOrEqualTo:zip];
[cityQ findObjectsInBackgroundWithBlock:^(NSArray *cObject, NSError *error) {
if (!error) {
PFObject *cityObject = [cObject lastObject];
PFObject *image = [objects lastObject];
[[image objectForKey:@"Image"] getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
if (!error) {
UIImage *image = [UIImage imageWithData:data];
NSMutableDictionary *flagDict = [[NSMutableDictionary alloc] init];
[flagDict setObject:[object objectForKey:@"title"] forKey:@"title"];
[flagDict setObject:image forKey:@"image"];
[flagDict setObject:[cityObject objectForKey:@"navn"] forKey:@"city"];
[flagDict setObject:[userName objectForKey:@"name"] forKey:@"name"];
[flagDict setObject:profileImage forKey:@"profileImage"];
[homesDic addObject:flagDict];
[self.collectionView reloadData];
} else {
// Log details of the failure
NSLog(@"Error: %@ %@", error, [error userInfo]);
}
}];
}
}];
}
}];
}];
}];
}
} else {
// Log details of the failure
NSLog(@"Error: %@ %@", error, [error userInfo]);
}
}];
}
答案 0 :(得分:0)
您传递给Parse框架查询方法的块是完成/回调块;当fetch完成后,它们就会运行。在获取后执行您需要执行的操作 - 包括在相应的完成块中调用cellHeights
。