我遇到的问题是只在第一次添加对象时才编辑sqlite文件。之后只更改了sqlite-shm文件。因此,当我重新启动应用程序时,没有任何变化。这是AppDelegate中的核心数据堆栈
lazy var managedObjectContext: NSManagedObjectContext = {
let modelURL = NSBundle.mainBundle().URLForResource("StoriesCoreData", withExtension: "mom")
let mom = NSManagedObjectModel(contentsOfURL: modelURL!)
let psc = NSPersistentStoreCoordinator(managedObjectModel: mom!)
let urls = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)
print (urls[urls.endIndex-1])
let storeURL = (urls[urls.endIndex-1]).URLByAppendingPathComponent("stories.sqlite")
var error: NSError? = nil
var store: NSPersistentStore?
do {
store = try psc.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: storeURL, options: nil)
} catch var error1 as NSError {
error = error1
store = nil
} catch {
fatalError()
}
if (store == nil) {
print("Failed to load store")
}
var managedObjectContext = NSManagedObjectContext()
managedObjectContext.persistentStoreCoordinator = psc
return managedObjectContext
}()
func saveContext() {
var error: NSError? = nil
let moc : NSManagedObjectContext? = self.managedObjectContext
if moc == nil {
return
}
if !managedObjectContext.hasChanges {
return
}
do {
try managedObjectContext.save()
return
} catch let error1 as NSError {
error = error1
}
print("Error saving context: \(error?.localizedDescription)\n\(error?.userInfo)")
abort()
}
以下是对象的初始化方式:
let aStory = NSEntityDescription.insertNewObjectForEntityForName("PersonalizedStory", inManagedObjectContext: managedObjectContext) as! PersonalizedStory
aStory.childrenMode = NSNumber(int: 0)
aStory.hidden = NSNumber(int: 0)
aStory.lastEdited = NSDate()
aStory.parentStoryDownloadDate = NSDate()
aStory.parentStoryID = "10000"
aStory.parentWriterID = "guest"
aStory.personalizedStoryID = "10"
aStory.pictureList = []
aStory.userID = "guest"
aStory.inSection = NSSet()
saveContext()
let aSection = NSEntityDescription.insertNewObjectForEntityForName("LibrarySection", inManagedObjectContext: managedObjectContext) as! LibrarySection
aSection.personalizedStoriesOrder = []
aSection.sectionName = "AllStories"
aSection.userID = "guest"
aSection.personalizedStoriesOrder!.append(aStory)
aSection.hasStory = NSSet()
saveContext()
aSection.addObject(aStory, forKey: "hasStory")
aStory.addObject(aSection, forKey: "inSection")
saveContext()
以下是获取请求和修改:
func allStories () -> [PersonalizedStory]? {
var error : NSError? = nil
let fetchStories = NSFetchRequest(entityName: "LibrarySection")
let sectionsForUser = NSPredicate(format: "userID = %@", argumentArray: [userID])
let all = NSPredicate(format: "sectionName == %@", argumentArray: ["AllStories"])
let fetchPredicates = NSCompoundPredicate(andPredicateWithSubpredicates: [sectionsForUser,all])
fetchStories.predicate = fetchPredicates
var result : [LibrarySection]?
do {
result = try appDelegate.managedObjectContext.executeFetchRequest(fetchStories) as? [LibrarySection]
} catch {
result = nil
}
let theSection = result!.first!.personalizedStoriesOrder!
for story in theSection {
story.setValue(1, forKey: "childrenMode")
}
appDelegate.saveContext()
return theSection
}
因此,当我稍后打印出故事时,childMode设置为1。
然而,当我重新启动时,每件事都被设置为0.
当我查看app文件夹
时