【问题标题】:App freezes with core data应用程序因核心数据而冻结
【发布时间】:2016-03-20 18:41:12
【问题描述】:

我正在开发类似于 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];
//    

我的应用卡住了..

有什么想法吗?

谢谢

【问题讨论】:

    标签: ios objective-c iphone core-data


    【解决方案1】:

    我的猜测是,如果您在后台进行下载(例如,多线程应用程序,这很可能是使用 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找到。

    【讨论】:

    • 先生,我是 ios 新手。可以给我举个例子吗
    猜你喜欢
    • 1970-01-01
    • 2011-07-25
    • 1970-01-01
    • 2011-12-10
    • 1970-01-01
    • 2016-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多