iOS 网络请求Json自动转存到CoreData(二)

项目需求:从网络获取Json后,将Json自动转存到CoreData中。

继上一篇日志,那么这篇的主要内容是:将Json存到CoreData中。

说话实话,无非就是KVC赋值,思路清晰明了,但是我在想一个问题,有没有办法做到通用呢?那么问题来了~挖机技术哪家强!

好了不扯淡了,虽然KVC暂时满足我项目需求,那个通用办法我还在寻找中,能力有限,不过我会努力。

顺便分享一篇 讲述 这个网络请求数据存放到CoreData的介绍

Process remote service data into Core Data

======================================================

Well, you have our data persisting to disk in a Property List format. But what you really want to do is to process it into Core Data. This is where you will be doing some heavy lifting and getting into the nitty gritty details. This is the part where the real magic happens!

To start off you will first need a way to retrieve the files from disk. Add -JSONDictionaryForClassWithName: in SDSyncEngine.m:

- (NSDictionary *)JSONDictionaryForClassWithName:(NSString *)className {
    NSURL *fileURL = [NSURL URLWithString:className relativeToURL:[self JSONDataRecordsDirectory]];
    return [NSDictionary dictionaryWithContentsOfURL:fileURL];
}

One caveat to the NSDictionary that -JSONDictionaryForClassWithName: returns is that the information you are interested in is will be in an NSArray with the key “results”. So to make things easier for processing purposes, add another method to access the data in the NSArray and spice it up a little to allow for sorting of the records by a specified key.

Add this beneath -JSONDictionaryForClassWithName: in SDSyncEngine.m:

- (NSArray *)JSONDataRecordsForClass:(NSString *)className sortedByKey:(NSString *)key {
    NSDictionary *JSONDictionary = [self JSONDictionaryForClassWithName:className];
    NSArray *records = [JSONDictionary objectForKey:@"results"];
    return [records sortedArrayUsingDescriptors:[NSArray arrayWithObject:
                                                 [NSSortDescriptor sortDescriptorWithKey:key ascending:YES]]];
}

This method calls the previous method you implemented, and returns an NSArray of all the records in the response, sorted by the specified key.

You won’t really need the JSON responses that were saved to disk much past this point, so add another method to delete them when you’re finished with them. Add the following method above -JSONDictionaryForClassWithName:

- (void)deleteJSONDataRecordsForClassWithName:(NSString *)className {
    NSURL *url = [NSURL URLWithString:className relativeToURL:[self JSONDataRecordsDirectory]];
    NSError *error = nil;
    BOOL deleted = [[NSFileManager defaultManager] removeItemAtURL:url error:&error];
    if (!deleted) {
        NSLog(@"Unable to delete JSON Records at %@, reason: %@", url, error);
    }
}

In order to translate records from JSON to NSManagedObjects, you will need a few methods. First, you will need to translate the JSON values to Objective-C properties; the method you use will vary based on the remote service you are working with. In this case, you are using Parse which has a few “special” data types. The data you’ll be concerned with here are Files and Dates. Files are returned as URLs to the file’s location, and Dates are returned in the following format:

{
  "__type": "Date",
  "iso": "2011-08-21T18:02:52.249Z"
}

Since the date is in the format of a string, you will want some methods to convert from a Parse formatted date string to an NSDate and back to an NSString. NSDateFormatter can help with this, but they are very expensive to allocate — so first add a new NSDateFormatter property that you can re-use.

Add the dateFormatter property in your private category:

@interface SDSyncEngine ()

@property (nonatomic, strong) NSMutableArray *registeredClassesToSync;
@property (nonatomic, strong) NSDateFormatter *dateFormatter;

@end

And add these three methods above the #pragma mark -File Management.

- (void)initializeDateFormatter {
    if (!self.dateFormatter) {
        self.dateFormatter = [[NSDateFormatter alloc] init];
        [self.dateFormatter setDateFormat:@"yyyy-MM-dd‘T‘HH:mm:ss‘Z‘"];
        [self.dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
    }
}

- (NSDate *)dateUsingStringFromAPI:(NSString *)dateString {
    [self initializeDateFormatter];
    // NSDateFormatter does not like ISO 8601 so strip the milliseconds and timezone
    dateString = [dateString substringWithRange:NSMakeRange(0, [dateString length]-5)];
    
    return [self.dateFormatter dateFromString:dateString];
}

- (NSString *)dateStringForAPIUsingDate:(NSDate *)date {
    [self initializeDateFormatter];
    NSString *dateString = [self.dateFormatter stringFromDate:date];
    // remove Z
    dateString = [dateString substringWithRange:NSMakeRange(0, [dateString length]-1)];
    // add milliseconds and put Z back on
    dateString = [dateString stringByAppendingFormat:@".000Z"];
    
    return dateString;
}

The first method -initializeDateFormatter will initialize your dateFormatter property. The second method -dateUsingStringFromAPI: receives an NSString and returns an NSDate object. The third method -dateStringForAPIUsingDate: receives an NSDate and returns an NSString.

Take a little closer look, there, detective — the second and third methods do something a little strange. Parse uses timestamps in the ISO 8601 format which do not translate to NSDate objects very well, so you need to do some stripping and appending of the milliseconds and Z flag (used to denote the timezone). (Oh standards…there are so many wonderful ones to choose from!) :]

Next add this method below mostRecentUpdatedAtDateForEntityWithName:

- (void)setValue:(id)value forKey:(NSString *)key forManagedObject:(NSManagedObject *)managedObject {
    if ([key isEqualToString:@"createdAt"] || [key isEqualToString:@"updatedAt"]) {
        NSDate *date = [self dateUsingStringFromAPI:value];
        [managedObject setValue:date forKey:key];
    } else if ([value isKindOfClass:[NSDictionary class]]) {
        if ([value objectForKey:@"__type"]) {
            NSString *dataType = [value objectForKey:@"__type"];
            if ([dataType isEqualToString:@"Date"]) {
                NSString *dateString = [value objectForKey:@"iso"];
                NSDate *date = [self dateUsingStringFromAPI:dateString];
                [managedObject setValue:date forKey:key];
            } else if ([dataType isEqualToString:@"File"]) {
                NSString *urlString = [value objectForKey:@"url"];
                NSURL *url = [NSURL URLWithString:urlString];
                NSURLRequest *request = [NSURLRequest requestWithURL:url];
                NSURLResponse *response = nil;
                NSError *error = nil;
                NSData *dataResponse = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
                [managedObject setValue:dataResponse forKey:key];
            } else {
                NSLog(@"Unknown Data Type Received");
                [managedObject setValue:nil forKey:key];
            }
        }
    } else {
        [managedObject setValue:value forKey:key];
    }
}

This method accepts a value, key, and managedObject. If the key is equal to createdDate or updatedAt, you will be converting them to NSDates. If the key is an NSDictionary you will check the __type key to determine the data type Parse returned. If it is a Date, you will convert the value from an NSString to an NSDate. If it is a File, you will do a little more work since you are interested in getting the image itself!

To get the image, send off a request to download the image file. It is important to note that downloading the image data can take a considerable amount of time, so this may only work efficiently with smaller data sets. Another solution would be to fetch the image data when the record is accessed (lazy loading), but it would only be available if the user has an Internet connection at the time of lazy loading.

If the data type is anything other than a File or Date there is no way to know what to do with it so set the value to nil. In any other case you will simply pass the value and key through untouched and set them on the managedObject.

后话:

耐心看到这里,其实我就是想证明一点,果然还是KVC!

时间: 2024-09-30 17:48:11

iOS 网络请求Json自动转存到CoreData(二)的相关文章

iOS 网络请求Json自动转存到CoreData(一)

奉上地址一枚:GitHub 并自带demo- 项目需求:从网络获取Json后,将Json自动转存到CoreData中. 本次任务:对CoreData封装,完成基本的增删改查,多线程操作. 我们总是在跌跌撞撞中,不断成长. CoreDataActivity.h // //  CoreDataActivity.h //  QGLCoreDataPlugin // //  Created by Guicai.Li on 14-10-16. //  Copyright (c) 2014年 Guicai 

ios网络请求3840错误的问题

ios网络请求3840错误的问题 by 伍雪颖 Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn't be completed. (Cocoa error 3840.)" (Unterminated string around character 11080.) UserInfo=0x171065d00 {NSDebugDescription=Unterminated string around charac

iOS网络请求框架:MKNetWorkKit的使用

MKNetWorkKit是由一个印度小伙子写的,是用于网络请求的库,支持ARC,我fork了一下,代码的网址这里给出. 本人fork作者代码地址(DoubleYi):https://github.com/DoubleYi/MKNetworkKit 作者源码地址(MugunthKumar):https://github.com/MugunthKumar/MKNetworkKit 作者关于类库介绍的地址(MugunthKumar):http://blog.mugunthkumar.com/produ

Android 网络请求json数据,解析json数据,生成对应的java bean类一步到位,快速开发

Android 网络请求一般都涉及到图片和JSON数据,怎样快速的请求网络JSON数据,解析JSON数据,并且一步生成自己想要的Java bean实体类?这个涉及到Android 开发效率的问题.由于接触Android 网络这方面比较多,自然就找到一些好的方法来快速开发Android 网络模块的相关内容,接下来就为大家揭晓 一步快速请求,解析JSON 数据生成对应的Java bean实体类的方法. 注:我们先把思路讲解下吧: 1.网络请求JSON数据代码可以自己写,当然我还是推荐使用网络上开源的

网络请求UI自动切换框架

1. 概述与分析 在实际项目中,我们不可避免的需要网络请求数据,由于网络或请求方式等主观或客观原因,导致我们请求的结果有时会出现一些偏差,从而导致我们UI界面显示也会有所不同.一般情况下,网络请求后我们的界面一般呈现三种页面状态:"正在加载中"."加载失败"和"加载成功".那么就可以通过网络请求后结果让UI界面自动切换, 2. 设计思路 通常情况下,正在加载和加载失败的界面都是相同的,只有加载成功后的界面不同.为了能让UI界面随着网络请求结果自动

iOS 网络请求NSURLSession

iOS 7 和 Mac OS X 10.9 Mavericks 中一个显著的变化就是对 Foundation URL 加载系统的彻底重构. 现在已经有人在深入苹果的网络层基础架构的地方做研究了,所以我想是时候来分享一些对于我对于这些新的 API 的看法和心得了,新的 API 将如何影响我们编写程序,以及它们对于 API 设计理念的影响. NSURLConnection 作为 Core Foundation / CFNetwork 框架的 API 之上的一个抽象,在 2003 年,随着第一版的 S

iOS 网络请求 笔记

有关网络请求的类(该图片来自:developer.apple.com) 一.url请求 网络请求的组成部分有服务器地址.请求参数,以及请求方式.在iOS中,一个网络请求用NSURLRequest(或者其子类NSURLMutableRequest)来表示. NSURLRequest适合于get请求,NSURLMutableRequest可以是post请求. get方式下,服务器地址和请求参数都可以明文包含在url中,可以直接使用这个url来创建NSURLRequest(或者NSURLMutable

造轮子 | 如何设计一个面向协议的 iOS 网络请求库

最近开源了一个面向协议设计的网络请求库 MBNetwork,基于 Alamofire 和 ObjectMapper 实现,目的是简化业务层的网络请求操作. 需要干些啥 对于大部分 App 而言,业务层做一次网络请求通常关心的问题有如下几个: 如何在任意位置发起网络请求. 表单创建.包含请求地址.请求方式(GET/POST/--).请求头等-- 加载遮罩.目的是阻塞 UI 交互,同时告知用户操作正在进行.比如提交表单时在提交按钮上显示 "菊花",同时使其失效. 加载进度展示.下载上传图片

swift 网络请求&json解析

做实际项目总是离不开这两步,大多数情况下都是采用第三方框架来辅助完成,swift也不例外,由于swift并不成熟,用swift语言写的第三方框架很不稳定(苹果更新太快了),所幸swift和oc可以混编,所以本次使用的框架都是oc里面常用的.网络请求使用AFNetworking,json解析使用MJExtension. 下面开始正文: 1. swift中使用oc框架 其原理很简单,需要一个.h头文件,里面导入我们需要使用的oc框架的头文件,把这个.h文件配置到项目设置中即可 1).我们采取一个不容