我实际上想在解析Json数据并创建childs对象,保存和管理关系时处理关系。
我使用findOrCreat函数来验证我的对象是否已经存在,是否存在,是否确实从JSON数据进行更新,如果不存在,则创建该对象。当我使用NSAsynchnonusFetchRequest时,我对如何处理关系一无所知。另外,必须在父对象之前创建关系,因此,当我在异步调用中创建父对象时,如何管理子对象和关系的创建?
在这里您可以找到我的findOrCreat函数:
// Asynchronous fetch
let asyncFetch = NSAsynchronousFetchRequest(fetchRequest: fetchRequest) {
(result:NSAsynchronousFetchResult!)-> Void in
if result.finalResult!.count <= 0 {
var eventToSave:Events!;
eventToSave = NSEntityDescription.insertNewObjectForEntityForName(Entities.Event.rawValue, inManagedObjectContext: self.cdhUser.backgroundContext!) as Events;
eventToSave.setValue(eventId, forKey: "id");
eventToSave.setValue((dictionary.objectForKey("title") as NSString), forKey: "title");
eventToSave.setValue((dictionary.objectForKey("exclusive") as Bool), forKey: "isExclusive");
eventToSave.setValue(0, forKey: "latitude");
eventToSave.setValue(0, forKey: "longitude");
eventToSave.setValue("", forKey: "details");
eventToSave.setValue((dictionary.objectForKey("summary") as NSString), forKey: "summary");
eventToSave.setValue((NSDate(timeIntervalSince1970: dictionary.objectForKey("date") as Double)), forKey: "date");
eventToSave.setValue(NSDate(), forKey: "validFrom");
eventToSave.setValue(NSDate(), forKey: "validTo");
// Get url of image
var pictureUrl:NSString!;
if let pictureDictionary:NSDictionary = dictionary.objectForKey("main_media") as? NSDictionary {
if let endUrlString:NSString = pictureDictionary.objectForKey("url") as? NSString {
pictureUrl = endUrlString;
}
}
eventToSave.setValue((pictureUrl != nil ? pictureUrl : ""), forKey: "picture"); // Default Image Url
self.cdhUser.saveContext(self.cdhUser.backgroundContext!);
}
else if result.finalResult!.count > 0 {
var eventToUpdate = result.finalResult?.first as Events;
eventToUpdate.setValue((dictionary.objectForKey("title") as NSString), forKey: "title");
eventToUpdate.setValue((dictionary.objectForKey("exclusive") as Bool), forKey: "isExclusive");
eventToUpdate.setValue(0, forKey: "longitude");
eventToUpdate.setValue(0, forKey: "latitude");
eventToUpdate.setValue("", forKey: "details");
eventToUpdate.setValue((dictionary.objectForKey("summary") as NSString), forKey: "summary");
eventToUpdate.setValue((NSDate(timeIntervalSince1970: dictionary.objectForKey("date") as Double)), forKey: "date");
eventToUpdate.setValue((NSDate()), forKey: "validTo");
eventToUpdate.setValue((NSDate()), forKey: "validFrom");
// Get url of image
var pictureUrl:NSString!;
if let pictureDictionary:NSDictionary = dictionary.objectForKey("main_media") as? NSDictionary {
if let endUrlString:NSString = pictureDictionary.objectForKey("url") as? NSString {
pictureUrl = endUrlString;
}
}
eventToUpdate.picture = pictureUrl;
println(eventToUpdate);
self.cdhUser.saveContext(self.cdhUser.backgroundContext!);
NSLog("Update is done!");
}
}
self.cdhUser.backgroundContext!.performBlock {
var result = self.cdhUser.backgroundContext!.executeRequest(asyncFetch, error: &error) as NSAsynchronousFetchResult;
}
}
这是我的JSON的一个示例:
{
"id": 1,
"title": "Inauguration",
"summary": "Evenement",
"main_media": {
"url": "c75b931abb131e9d091882c3638aca9fe0ecdee6.jpeg",
"name": "99.JPG",
"type": 2,
"created_at": 1415377363
},
"date": 1412174799,
"duration": 6483600,
"exclusive": true,
"geo_location": [],
"open": true,
"description": "Evenement",
"interested": true,
"medias": [{
"url": "1c3f251b7bccd3bd4337852538fe7ba0accefa77.jpeg",
"name": "b90mq.jpg",
"type": 2,
"created_at": 1415375677,
"main_media": false
}, {
"url": "c75b931abb131e9d091882c3638aca9fe0ecdee6.jpeg",
"name": "woodstock-99.JPG",
"type": 2,
"created_at": 1415377363,
"main_media": true
}]
}
因此,如何在创建事件对象的同时创建媒体对象?我什么时候可以调用findOrCreate函数来确定Medias Object?以及如何处理关系?
非常感谢
您应该在从事件到媒体的xcdatamodeld中添加一对多关系。
let manyRelation = eventToSave.valueForKeyPath("medias") as NSMutableSet
for dict : NSDictionary in dictionary.objectForKey("main_media") as? [NSDictionary] {
let media : Media = Media()
media.url = dict.objectForKey("url")
media.name = dict.objectForKey("name")
....
manyRelation.addObject(media)
}
在创建根对象之后和保存上下文之前,需要添加此代码
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句