ios - Blocking call in parse's queryfortable -
i have activity class, theme class , photo class. if there follow activity need find out theme user follows, photos tagged theme. here code:
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]; (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];
this code works findobjects blocking call app hangs bit while waiting. how rewrite query ?
instead of using findobjects
, use findobjectsinbackgroundwithblock:
executed asynchronously , not block main thread.
so code this:
pfquery *themequery = [pfquery querywithclassname:@"occasion"]; // ... add filters ... [themequery findobjectsinbackgroundwithblock:^(nsarray *results, nserror *error) { nslog(@"results: %@", results); }];
[update] here additional sample code query, compiled in head might need tweaking.
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"]; (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) { // final results }]; }];
Comments
Post a Comment