2014-04-07 49 views
0

嗨善良的人我想,以防止iOS版 - 應用程序凍結當我獲取請求死鎖

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秒後的高分,當我運行這個要求我的應用程序凍結(死鎖) 。 我來自保加利亞,我正在努力學習目標C.在這裏,我們沒有學校,因爲我們的國家在所有情況下都非常糟糕,保加利亞在歐洲聯盟中排在最後...... 一些認真和善於幫助的人我與我的代碼或解釋如何在Objective C中工作的一切,或只幫助我呢?

+0

您是否在應用程序中使用線程?如果你瞭解他們是什麼? –

+0

你有Skype幫助我,我會在這裏發佈答案,我會向不知道像我這樣的人解釋:) 我的Skype是:smoka_bogdan –

+0

抱歉不是現在,我在辦公室。可能在某個時間晚些時候。 –

回答

0

試試看看這個代碼。爲了儘可能簡化應用程序,請勿將Core數據代碼從主線程中取出,即與self.managedObjectContext相關的任何事情,例如保存或執行提取請求。這是因爲核心數據不是線程安全的,您將不得不設置一個策略來處理它。我假設你的申請很簡單,你不需要這樣的策略。因此,請儘量保持簡單,並始終在主線程上執行核心數據操作(保存,執行)。 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; 

    }); 
} 
+0

此代碼也凍結... –

+0

嗯。我認爲比你有一些在覈心數據上執行代碼的線程。有沒有什麼辦法可以分享你的代碼,以便我可以查看它,如果可能的話? –

+0

http://bbogdanov.us/hilo/index.txt here mate :) 請給我一個電子郵件或一些聯繫方式。 我想親自聯繫你:) http://facebook.com/bbogdanov –