取消PFQuery需要花费太多时间来加载

时间:2015-09-21 10:57:57

标签: ios parse-platform pfquery

道歉,我想不出更好的方式来描述我的 应用程序的功能。

我在这个主题上发现了很多帖子,也在parse.com的旧档案中。然而,这对我不起作用。创建由 PFQuery.findObjects 触发的 PFQuery 实例(但在后台线程上运行)后,我无法在请求过程中取消它。 / p>

场景:基本上我有一个连接到Parse的应用。我在DataDisplay屏幕上显示了超过100条记录的数据,当用户点击后退按钮时它有一个后退按钮,如果 PFQuery.findObjects 仍然在后台线程上运行它,那么我必须取消它

我尝试在viewWillDisappear中插入 PFQuery.cancel ,但它无法停止,并且由于这些DataDisplay屏幕的 dealloc 方法未被调用。

我的代码,包含它可能会有所帮助:

- (void)loadANDSortingSongInformationWS {

if(ISINTERNET) {

    if(self.isShowLoadingForSkipSong)//Not Showing the activity indicator
        self.isShowLoadingForSkipSong = NO;

    else if(self.isFirstLoad || self.isAddPullToRefreshLikeSong)//Showing the indicator
        [self showHideLoading:YES];

    PFQuery *query = [PFQuery queryWithClassName:[UserPlaylistSongs parseClassName]];
    [query setLimit:1000];
    [query orderByDescending:@"createdAt"];
    [query whereKey:@"Playlist" equalTo:self.playlistInfo];
    [query includeKey:@"Playlist"];
    [query includeKey:@"Song"];
    [query includeKey:@"AddedBy"];
    [query includeKey:@"Host"];

    __weak typeof(self) weakSelf = self;

    [self.opearation addOperationWithBlock:^{

        [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {

            if (objects.count == 0) {//No Songs Found

                //If there is no records
                dispatch_async(dispatch_get_main_queue(), ^{

                    [weakSelf showHideLoading:NO];

                    if(weakSelf.isFirstLoad || weakSelf.isAddPullToRefreshLikeSong) {//Problem while user pull to refresh when there is no song

                        [KSToastView ks_showToast:@"No Songs Found." duration:1.0f];
                    }
                });
            }

            else {//Songs Found

                dispatch_async(dispatch_get_main_queue(), ^{

                    NSMutableArray *arrParseSongList = [[NSMutableArray alloc] init];
                    __block NSInteger getTotalObjectsCount = 0;

                    for(NSInteger i=0; i<objects.count; i++) {

                        SongListInfo *songListData = [[SongListInfo alloc] init];
                        songListData.userPlaylistInfo = objects[i];

                        songListData.totLikes = [objects[i][@"Likes"] integerValue];
                        songListData.totDisLikes = [objects[i][@"Dislikes"] integerValue];
                        songListData.isPlaying = [objects[i][@"PlayingStatus"] boolValue];
                        songListData.songInfo = objects[i][@"Song"];
                        songListData.hostInfo = objects[i][@"Host"];
                        songListData.addedInfo = objects[i][@"AddedBy"];
                        songListData.playlistInfo = objects[i][@"Playlist"];
                        songListData.alreadyPlayedOrder = [objects[i][@"AlreadyPlayedIndex"] integerValue];
                        songListData.totRating = songListData.totLikes - songListData.totDisLikes;
                        songListData.createdDate = songListData.userPlaylistInfo.createdAt;

                        //User Specific for loading the song list.
                        PFQuery *queryLikeDislike = [PFQuery queryWithClassName:[SongLikeDislike parseClassName]];
                        [queryLikeDislike whereKey:@"SongID" equalTo:songListData.songInfo.objectId];
                        [queryLikeDislike whereKey:@"UserID" equalTo:[SINGLETON getUserID]];
                        [queryLikeDislike whereKey:@"PlaylistID" equalTo:songListData.playlistInfo.objectId];

                        [queryLikeDislike findObjectsInBackgroundWithBlock:^(NSArray *objectsLike, NSError *error) {

                            getTotalObjectsCount += 1;

                            if(error == nil) {

                                if(objectsLike.count) {

                                    BOOL isDelete = [objectsLike.lastObject[@"DeleteRecord"] boolValue];
                                    BOOL isLike = [objectsLike.lastObject[@"Like"] boolValue];

                                    if(isDelete)
                                        songListData.ratingType = RATING_GRAY;
                                    else if(isLike)
                                        songListData.ratingType = RATING_GREEN;
                                    else
                                        songListData.ratingType = RATING_RED;
                                }

                                else
                                    songListData.ratingType = RATING_GRAY;
                            }

                            else
                                NSLog(@"Problem while getting the rating type");

                            [arrParseSongList addObject:songListData];

                            NSLog(@"i : %ld, objects : %ld",(long)getTotalObjectsCount, (long)objects.count);
                            if(getTotalObjectsCount == objects.count)
                                [weakSelf processAfterGettingLikesAndDislikeInfo:arrParseSongList];
                        }];
                    }
                });
            }
        }];
    }];

    NSLog(@"In method -> All operation : %ld",(long)self.opearation.operations.count);
}

else
    [UIAlertView showErrorWithMessage:NO_INTERNET handler:nil];
}

- (void)processAfterGettingLikesAndDislikeInfo:(NSMutableArray *)arrParseSongList {

NSPredicate *filterGrayout = [NSPredicate predicateWithFormat:@"isPlaying == YES"];
NSArray *arrGrayOut = [arrParseSongList filteredArrayUsingPredicate:filterGrayout];

NSSortDescriptor *aSortDescriptorGrayedOut = [[NSSortDescriptor alloc] initWithKey:@"alreadyPlayedOrder.intValue" ascending:YES];
NSArray *arrGrayedOutSong = [NSMutableArray arrayWithArray:[arrGrayOut sortedArrayUsingDescriptors:[NSArray arrayWithObject:aSortDescriptorGrayedOut]]];

NSPredicate *filterNonPlay = [NSPredicate predicateWithFormat:@"isPlaying == NO"];
NSArray *arrNonPlay = [arrParseSongList filteredArrayUsingPredicate:filterNonPlay];

NSSortDescriptor *aSortDescriptorRating = [[NSSortDescriptor alloc] initWithKey:@"totRating.intValue" ascending:NO];
NSSortDescriptor *aSortDescriptorCreatedDate = [[NSSortDescriptor alloc] initWithKey:@"createdDate" ascending:YES];

NSArray *arrSortOnNormalSong = [NSMutableArray arrayWithArray:[arrNonPlay sortedArrayUsingDescriptors:[NSArray arrayWithObjects:aSortDescriptorRating,aSortDescriptorCreatedDate,nil]]];

if(self.arrSongsData.count)
    [self.arrSongsData removeAllObjects];

[self.arrSongsData addObjectsFromArray:arrGrayedOutSong];
[self.arrSongsData addObjectsFromArray:arrSortOnNormalSong];

[self showHideLoading:NO];
[self.tblView reloadData];
}

我在viewWillDisappear中调用。

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];

    if(self.queryInstance)
       [self.queryInstance cancel];
}

感谢您的帮助!

0 个答案:

没有答案