我有一个活动类,一个主题类和一个照片类。如果有跟随活动,我需要找出用户遵循的主题,然后用该主题标记哪些照片。 这是我的代码:
PFQuery *themeQuery = [PFQuery queryWithClassName:@"Occasion"];
PFQuery *followingActivitiesQuery = [PFQuery queryWithClassName:kPAPActivityClassKey];
[followingActivitiesQuery whereKey:kPAPActivityFromUserKey equalTo:[PFUser currentUser]];
[followingActivitiesQuery whereKey:@"type" equalTo:@"followtheme"];
[followingActivitiesQuery includeKey:@"themePointer"];
NSArray *objects = [followingActivitiesQuery findObjects];
for (PFObject *object in objects) {
PFObject *tobject = object[@"themePointer"];
NSString *themename = [tobject valueForKey:@"name"];
[themeQuery whereKey:@"name" equalTo: themename];
}
PFQuery *photosFromFollowedThemeQuery = [PFQuery queryWithClassName:self.parseClassName];
[photosFromFollowedThemeQuery whereKey:@"themeRelation" matchesQuery:themeQuery];
此代码有效,但findobjects是一个阻塞调用,所以我的应用程序在等待时会挂起一点。如何重写此查询?
答案 0 :(得分:0)
不使用findObjects
,而是使用异步执行的findObjectsInBackgroundWithBlock:
并且不阻止主线程。
所以你的代码看起来像这样:
PFQuery *themeQuery = [PFQuery queryWithClassName:@"Occasion"];
// ... add filters ...
[themeQuery findObjectsInBackgroundWithBlock:^(NSArray *results, NSError *error) {
NSLog(@"Results: %@", results);
}];
[更新] 以下是您查询的一些其他示例代码,我在脑海中编译了这些代码,因此可能需要进行一些调整。
PFQuery *followingActivitiesQuery = [PFQuery queryWithClassName:kPAPActivityClassKey];
[followingActivitiesQuery whereKey:kPAPActivityFromUserKey equalTo:[PFUser currentUser]];
[followingActivitiesQuery whereKey:@"type" equalTo:@"followtheme"];
[followingActivitiesQuery includeKey:@"themePointer"];
[followingActivitiesQuery findObjectsInBackgroundWithBlock:^(NSArray *results, NSError *error) {
PFQuery *themeQuery = [PFQuery queryWithClassName:@"Occasion"];
for (PFObject *object in results) {
PFObject *tobject = object[@"themePointer"];
NSString *themename = [tobject valueForKey:@"name"];
[themeQuery whereKey:@"name" equalTo: themename];
}
PFQuery *photosFromFollowedThemeQuery = [PFQuery queryWithClassName:self.parseClassName];
[photosFromFollowedThemeQuery whereKey:@"themeRelation" matchesQuery:themeQuery];
[photosFromFollowedThemeQuery findObjectsInBackgroundWithBlock:^(NSArray *finalResults, NSError *finalError) {
// Do something with the final results
}];
}];