Ios 将核心数据与Web服务同步

Ios 将核心数据与Web服务同步,ios,objective-c,json,core-data,parse-platform,Ios,Objective C,Json,Core Data,Parse Platform,我有一个关于CoreData和RESTfulWeb服务的问题。 我阅读了这篇关于本地CoreData和远程数据同步的教程。 我无法理解,因为在方法中: - (void)downloadDataForRegisteredObjects:(BOOL)useUpdatedAtDate 这些信息在保存到JSON文件之前总是先保存 使用以下方法:[self-writeJSONResponse:responseObject toDiskForClassWithName:className] 当所有操作完成

我有一个关于CoreData和RESTfulWeb服务的问题。 我阅读了这篇关于本地CoreData和远程数据同步的教程。 我无法理解,因为在方法中:

- (void)downloadDataForRegisteredObjects:(BOOL)useUpdatedAtDate
这些信息在保存到JSON文件之前总是先保存

使用以下方法:[self-writeJSONResponse:responseObject toDiskForClassWithName:className]

当所有操作完成后,就可以将这些存储在CoreData上

这有什么潜在的动机吗

为什么我们不能直接在CoreData上保存以删除读/写文件所增加的开销?
谢谢

tl;dr此保存到磁盘可能是不必要的开销


我无法评论作者的动机,但我怀疑这是因为该应用程序是作为教程构建的,因此“保存到文件”将有关从Parse.com下载数据的部分与关于将JSON解析为CoreData的部分分开。

在将值应用于托管对象和,如果您确实将JSON文件写出来,甚至写到caches目录,那么完成后应该将它们删除

下面是如何将来自web服务响应的JSON数据应用于核心数据管理对象

本文使用AFHTTPRequestOperation,因此我们将在这里使用它。请注意,我假设您可以通过某种方式获取正在应用JSON数据的托管对象。通常,这将使用模式完成

我假设SDAFParseAppicClient已经解析了JSON。我们检查以确保解析的JSON是NSDictionary,然后使用它将其应用于托管对象

使用NSURLConnection做同样的事情很简单,而且可能是更好的学习体验。其他基础网络方法NSURLIST等也会起到同样的作用:

[NSURLConnection sendAsynchronousRequest:request queue:queue completion:(NSURLResponse *response, NSData *data, NSError *error)]
    NSIndexSet          *acceptableStatusCodes  =  [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(200, 99)];

    if ([acceptableStatusCodes containsIndex:[(NSHTTPURLResponse *)response statusCode]]){
        if ([data length] > 0){
            // Parse the JSON
            id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
            if (json != nil && [json respondsToSelector:@selector(objectForKey:)]){
                // Set the JSON values on the managed object, assuming the managed object properties map directly to the JSON keys
                [managedObject setValuesForKeysWithDictionary:json];
            } else {
                // handle the error
            }
        }
    } else {
        // Handle the error
    }
}];
我们发送一个带有完成块的异步请求。向该块传递NSHTTPPURLRESPONSE、NSData和NSError。首先,我们检查响应的状态码是否在200“OK”范围内。如果不是,或者响应为nil,则可能传递了一个NSError,说明了原因。如果响应在200范围内,则在将其交给NSJSONSerialization之前,我们确保NSData中包含某些内容。解析JSON对象后,我们确保它响应相关的NSDictionary方法,然后使用键值编码将值应用于托管对象。这假设JSON键和值直接映射到托管对象的属性——如果它们没有映射到托管对象的属性,那么您可以使用许多选项来重新映射或转换键和值,这些选项甚至超出了本问题的范围

[NSURLConnection sendAsynchronousRequest:request queue:queue completion:(NSURLResponse *response, NSData *data, NSError *error)]
    NSIndexSet          *acceptableStatusCodes  =  [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(200, 99)];

    if ([acceptableStatusCodes containsIndex:[(NSHTTPURLResponse *)response statusCode]]){
        if ([data length] > 0){
            // Parse the JSON
            id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
            if (json != nil && [json respondsToSelector:@selector(objectForKey:)]){
                // Set the JSON values on the managed object, assuming the managed object properties map directly to the JSON keys
                [managedObject setValuesForKeysWithDictionary:json];
            } else {
                // handle the error
            }
        }
    } else {
        // Handle the error
    }
}];