iOS - 当我获取请求死锁时应用程序冻结

时间:2014-04-07 01:33:50

标签: ios core-data request freeze

嗨好人我试图用

来阻止冻结
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{ CODE });

但我不知道如何使用函数..我不知道在哪里放置managedObjectContext以及如何使用此dispatch_async我的代码是:

- (void)updateFacebookFriendsHighScore{
    NSFetchRequest *requestche =[NSFetchRequest fetchRequestWithEntityName:@"Time"];
    [requestche setReturnsObjectsAsFaults:NO];
    NSPredicate *predicate=[NSPredicate predicateWithFormat:@"timeid==1"];
    requestche.predicate=predicate;
    NSArray *getIDTime = [self.managedObjectContext executeFetchRequest:requestche error:nil];
    NSString *getTheTime = [[getIDTime valueForKey:@"time"] componentsJoinedByString:@""];
    NSNumber *timeInInt = [NSNumber numberWithInteger: [getTheTime intValue]];
    int timeFromDB = [timeInInt intValue];
    timeFromDB = timeFromDB + 509;
    int timeNow = [[NSDate date] timeIntervalSince1970];
    if(timeNow > timeFromDB){
        NSFetchRequest *updateHighScoreRequest = [NSFetchRequest fetchRequestWithEntityName:@"Friends"];
        [updateHighScoreRequest setReturnsObjectsAsFaults:NO];
        NSArray *friendsToUpdate = [self.managedObjectContext executeFetchRequest:updateHighScoreRequest error:nil];
        for(NSArray *friendId in friendsToUpdate){
            NSString *getFriendId = [friendId valueForKey:@"fbid"] ;
            NSString *siteURL = [NSString stringWithFormat:@"http://www.example.com/example.php?fbid=%@", getFriendId];
            NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:siteURL]];
            [NSURLConnection sendAsynchronousRequest:urlRequest queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
                NSString *resultsFromDB = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
                NSNumber *theScoreForUpdate = [NSNumber numberWithInt:[resultsFromDB intValue]];
                NSFetchRequest *updateTheHighScoreRequest = [NSFetchRequest fetchRequestWithEntityName:@"Friends"];
                NSPredicate *updateTheHighScorePredicate = [NSPredicate predicateWithFormat:@"fbid==%@",getFriendId];
                updateTheHighScoreRequest.predicate=updateTheHighScorePredicate;
                Friends *setScore = [[self.managedObjectContext executeFetchRequest:updateTheHighScoreRequest error:nil] lastObject];
                NSLog(@"%@", setScore);
                [setScore setValue:theScoreForUpdate forKey:@"score"];
                [self.managedObjectContext save:nil];
                data = nil;
                resultsFromDB = nil;
                theScoreForUpdate = nil;
                setScore = nil;
         }];
        updateHighScoreRequest = nil;
        }
    }
    requestche = nil;
}

此代码从数据库获取时间并在CD结果509秒后更新高分,当我运行此请求时,我的应用程序冻结(DEADLOCK)。 我来自保加利亚,我正在努力学习目标C.在这里,我们没有学校,因为我们的国家在所有情况下都是非常糟糕的,保加利亚在欧盟是最后的... 一些认真善良的人可以帮助我解决我的代码或解释如何在Objective C中运行所有内容或者仅仅帮助我吗?

1 个答案:

答案 0 :(得分:0)

试试这段代码。为了使应用程序尽可能简单,请不要将Core数据代码从主线程中取出,即与self.managedObjectContext相关的任何内容,例如保存或执行获取请求。这是因为Core数据不是线程安全的,您必须设置策略来处理它。我假设你的申请是直截了当的,你不需要这样的策略。因此,请尽量保持简单,并始终在主线程上执行核心数据操作(保存,执行)。 dispatch_async(dispatch_get_main_queue(), ^{ code });将在主线程上执行它。

-(void) updateFacebookFriendsHighScore
{
    dispatch_async(dispatch_get_main_queue(), ^{

        NSFetchRequest *requestche =[NSFetchRequest fetchRequestWithEntityName:@"Time"];
        [requestche setReturnsObjectsAsFaults:NO];
        NSPredicate *predicate=[NSPredicate predicateWithFormat:@"timeid==1"];
        requestche.predicate=predicate;
        NSArray *getIDTime = [self.managedObjectContext executeFetchRequest:requestche error:nil];
        NSString *getTheTime = [[getIDTime valueForKey:@"time"] componentsJoinedByString:@""];
        NSNumber *timeInInt = [NSNumber numberWithInteger: [getTheTime intValue]];
        int timeFromDB = [timeInInt intValue];
        timeFromDB = timeFromDB + 509;
        int timeNow = [[NSDate date] timeIntervalSince1970];
        if(timeNow > timeFromDB){
            NSFetchRequest *updateHighScoreRequest = [NSFetchRequest fetchRequestWithEntityName:@"Friends"];
            [updateHighScoreRequest setReturnsObjectsAsFaults:NO];
            NSArray *friendsToUpdate = [self.managedObjectContext executeFetchRequest:updateHighScoreRequest error:nil];
            for(NSArray *friendId in friendsToUpdate){
                NSString *getFriendId = [friendId valueForKey:@"fbid"] ;
                NSString *siteURL = [NSString stringWithFormat:@"http://www.example.com/example.php?fbid=%@", getFriendId];
                NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:siteURL]];
                [NSURLConnection sendAsynchronousRequest:urlRequest queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
                    NSString *resultsFromDB = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
                    NSNumber *theScoreForUpdate = [NSNumber numberWithInt:[resultsFromDB intValue]];
                    NSFetchRequest *updateTheHighScoreRequest = [NSFetchRequest fetchRequestWithEntityName:@"Friends"];
                    NSPredicate *updateTheHighScorePredicate = [NSPredicate predicateWithFormat:@"fbid==%@",getFriendId];
                    updateTheHighScoreRequest.predicate=updateTheHighScorePredicate;
                    Friends *setScore = [[self.managedObjectContext executeFetchRequest:updateTheHighScoreRequest error:nil] lastObject];
                    NSLog(@"%@", setScore);
                    [setScore setValue:theScoreForUpdate forKey:@"score"];
                    [self.managedObjectContext save:nil];
                    data = nil;
                    resultsFromDB = nil;
                    theScoreForUpdate = nil;
                    setScore = nil;
                }];
                updateHighScoreRequest = nil;
            }
        }
        requestche = nil;

    });
}