標籤:des style color io os ar for strong sp
項目需求:從網路擷取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!
iOS 網路請求Json自動轉存到CoreData(二)