应用冻结核心数据

时间:2015-12-15 04:51:17

标签: ios objective-c iphone core-data

我正在开发类似于getPocket的iOS应用。我有新闻应用程序供以后阅读。

我在第一次通话中获取所有新闻列表并保存到coredata中。我也开始下载新闻的全部内容并试图保存到coredata。

根据要求,我们不需要分页。

我正在使用此代码

NSDictionary *params =@{
                                @"access_token"  :str_AccessToken,
                                @"bookmark_file" :str_BookmarkFile,
                                @"alternate_id"  :str_ALTID
                                };

        //============================AFNETWORKING HEADER=========================================
        //
        //==========================AFNETWORKING HEADER==================================
        AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
        httpClient.parameterEncoding = AFFormURLParameterEncoding;
        [httpClient registerHTTPOperationClass:[AFJSONRequestOperation class]];
        [httpClient setDefaultHeader:@"Accept" value:@"application/json"];

        //===============================SIMPLE REQUEST=====================
        NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST"
                                                                path:kGetArticleDetail
                                                          parameters:params];
        //====================================================RESPONSE
        AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
        [operation setUploadProgressBlock:^(NSUInteger bytesWritten,long long totalBytesWritten, long long totalBytesExpectedToWrite) {

        }];
        // Get the data
        [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSError *error = nil;
            NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:&error];
            [delegate.activityIndicator stopAnimating];
            self.view.userInteractionEnabled=YES;
            [self ResponseDetail:JSON];
        }
         //==================================================ERROR
                                         failure:^(AFHTTPRequestOperation operation, NSError error) {
                                             [delegate.activityIndicator stopAnimating];
                                             if([operation.response statusCode] == 406){

                                                 //[SVProgressHUD showErrorWithStatus:@"Not Login"];
                                                 return;
                                             }

                                             if([operation.response statusCode] == 403){
                                                 NSLog(@"Upload Failed");
                                                 return;
                                             }
                                             if ([[operation error] code] == -1009) {
                                                 UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"mywebsite"
                                                                                              message:@"Please check your internet connection"
                                                                                             delegate:nil
                                                                                    cancelButtonTitle:@"OK"
                                                                                    otherButtonTitles:nil];
                                                 [av show];
                                             }
                                             else if ([[operation error] code] == -1001) {
                                                 [self aFGetReaderDetail:str_BookmarkFile string:str_ALTID];
                                             }
                                         }];
        [operation start];

和核心数据代码

delegate=(AppDelegate *)[[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *context=[delegate managedObjectContext];
    NSEntityDescription *entityDesc=[NSEntityDescription entityForName:@"Save_Reader" inManagedObjectContext:context];
//    NSPersistentStoreCoordinator *persistentStoreCoordinator = [context persistentStoreCoordinator];
//    context = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
//    

我的应用程序冻结..

有什么想法吗?

由于

1 个答案:

答案 0 :(得分:2)

我的猜测是,如果您在后台进行下载(例如多线程应用程序,可能就是使用AFNetworking的情况),则需要为执行该工作的线程创建一个新的managedObjectContext。您现在正在做的是获取存在于您的应用程序委托中的managedObjectContext。

而不是:

delegate=(AppDelegate *)[[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context=[delegate managedObjectContext];

做类似的事情:

appDelegate=(AppDelegate *)[[UIApplication sharedApplication] delegate];
NSPersistentStoreCoordinator *coordinator = [appDelegate persistentStoreCoordinator];
// save this managedObjectContext as a property of your view controller,
// so it doesn't get released by ARC
managedObjectContext = [[NSManagedObjectContext alloc] init];
[managedObjectContext setPersistentStoreCoordinator: coordinator];

可以找到更多信息in this related question