嗨善良的人我想,以防止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中工作的一切,或只幫助我呢?
您是否在應用程序中使用線程?如果你瞭解他們是什麼? –
你有Skype幫助我,我會在這裏發佈答案,我會向不知道像我這樣的人解釋:) 我的Skype是:smoka_bogdan –
抱歉不是現在,我在辦公室。可能在某個時間晚些時候。 –