【问题标题】:MagicalRecord import only saving last itemMagicalRecord 导入仅保存最后一项
【发布时间】:2013-09-09 16:01:22
【问题描述】:

我有一个 AFNetworking 调用,它正在调用我的 API 并存储结果。我有一个类似的电话,以这种方式工作得很好。但是对于这个,它似乎只存储最后一项。

[client getPath:@"GetItemsByFilter/" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"Operation: %@", operation);
        NSLog(@"Response: %@", responseObject);

        NSArray *customerFieldResults = [responseObject valueForKeyPath:@"details.items"];
        NSLog(@"Results: %@", customerFieldResults);
        __block NSArray *array;
        @try {
            [CustomerFields MR_truncateAll];
            array = [CustomerFields MR_importFromArray:customerFieldResults];
            NSLog(@"done setting array: %@", array);


        } @catch (NSException *e) {
            NSLog(@"Exception: %@", e);
        } @finally {
            NSLog(@"tc done");

        }
        NSLog(@"Array 1: %@", array);
        [[NSNotificationCenter defaultCenter] postNotificationName:kCustomerFieldSetComplete object:nil];

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Operation: %@", operation);
        NSError *jsonError;
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:[[error localizedRecoverySuggestion] dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:&jsonError];
        NSLog(@"Error: %@", [error localizedRecoverySuggestion]);

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"Api Error: %@", [dict valueForKey:@"status"]] message:[dict valueForKey:@"statusMessage"] delegate:nil cancelButtonTitle:NSLocalizedString(@"Ok", @"Okay button") otherButtonTitles:nil];

        [alert show];
    }];

当我使用上下文执行此操作时,它甚至不会保存项目。收到此信息完成后,我将发送通知,并在该通知中显示来自[CustomerFields findAll] 的结果。仅找到一项带有 [CustomerFields findAll] 的项目。

Array1 显示了完整的项目列表,但是一旦我回到另一个控制器,它只会返回数组中的最后一个项目。此外,如果我将其包装在 saveWithBlock 中,它将不会在另一个控制器中看到任何项目。

为什么当我执行findAll 时它只显示导入的最后一条记录?

【问题讨论】:

    标签: ios objective-c afnetworking magicalrecord


    【解决方案1】:

    我对 Magical Record 不太熟悉,但也许您没有保存核心数据上下文?

    我假设MR_importFromArray:方法将记录插入CoreData?

    如果不是这样,您可能希望循环遍历您的结果并一个一个地创建它们?

    [client getPath:@"GetItemsByFilter/" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject)
    {
        NSArray *customerFieldResults = [responseObject valueForKeyPath:@"details.items"];
    
        customerFieldResults = [CustomerFields MR_importFromArray:customerFieldResults];
    
        //loop through array and create records
    
        //save your core data context here
    
        [[NSNotificationCenter defaultCenter] postNotificationName:kCustomerFieldSetComplete object:nil];
    }
    failure:^(AFHTTPRequestOperation *operation, NSError *error){}];
    

    【讨论】:

      【解决方案2】:

      我想通了。事实证明,我使用的 API 发生了变化,而我作为 relatedByAttribute 使用的字段返回空白(一个错误),因此它认为我的所有记录都是重复的并覆盖它们。

      【讨论】:

        猜你喜欢
        • 2014-07-09
        • 1970-01-01
        • 2012-10-25
        • 2017-05-28
        • 2017-08-16
        • 1970-01-01
        • 1970-01-01
        • 2017-11-03
        • 1970-01-01
        相关资源
        最近更新 更多