我正在尝试从数组中保存核心数据对象,并在检索它时,我得到的对象没有更新的值。
我相信在保存时我做错了什么。
这是代码:
func getAllHomeSectionsData(entity entity : NSString, data : NSArray) -> NSMutableArray {
let context: NSManagedObjectContext = DKCoreDataManager.mainManagedObjectContext()
data.enumerateObjectsUsingBlock({ object, index, stop in
let homeSectionObject : NSDictionary = object as! NSDictionary
let homeSections = HomeSections.insertInManagedObjectContext(managedObjectContext:context , entity: entity) as! HomeSections
// Saving Section ID
let homeSectionID = homeSectionObject.objectForKey("id" )?.integerValue
homeSections.id = NSNumber(integer : homeSectionID!)
// Saving Section Type
let homeSectionType = homeSectionObject.objectForKey("type" )?.integerValue
homeSections.type = NSNumber(integer : homeSectionType!)
// Saving URL
homeSections.url = homeSectionObject.objectForKey("url") as! NSString as String
})
do {
try context.save()
} catch {
fatalError("Failure to save context: \(error)")
}
let array : NSMutableArray = DKCoreDataManager.getObjectsForEntity(entityName: entity, sortAscending: true, managedObjectContext:DKCoreDataManager.mainManagedObjectContext())
return array
}
有人可以建议我如何解决这个问题吗?