1

Having a strange issue with Core Data in a Swift app. I'm reasonably well-versed in Core Data but it seems like there are some (new) rough edges and "gotchas" when it comes to using it in Swift.

I can insert new objects into the context just fine, and retrieve them from the same context elsewhere in my app while running. However, whenever I try to save the context, it fails for no apparent reason (managedObjectContext.save() returns false). When I log or inspect the error pointer, it's 0x0000000...so I'm not getting any hints as to what's gone wrong.

func saveContext() {
    let error = NSErrorPointer()

    if (!self.managedObjectContext.save(error)) {
        println("*** Failed to save MOC with error: \(error.debugDescription)")
    }
}

I set a breakpoint and inspected the context, partial results below...I also po'd the 'insertedObjects' property and did, indeed, find all the data I was trying to save. Everything looked fine there.

Why won't the context save? I realize that with no error or exception there's not a lot to go off, but I'm hoping someone has encountered this and either knows a fix or at least can point me in the right direction. Thanks :)

Context in debugger

I turned on SQL debugging and get the following when I try to save. I'm not a SQL expert by any means but this looks like something's missing. Namely, I don't see any INSERT statements. Also, curiously, this output only gets generated the first time I try to add a bookmark when launching the app. After that, it's mum.

2015-03-23 14:19:13.152 NTBrowser[3535:157990] CoreData: sql: SELECT TBL_NAME FROM SQLITE_MASTER WHERE TBL_NAME = 'Z_METADATA' 2015-03-23 14:19:13.152 NTBrowser[3535:157990] CoreData: sql: pragma journal_mode=wal 2015-03-23 14:19:13.152 NTBrowser[3535:157990] CoreData: sql: pragma cache_size=200 2015-03-23 14:19:13.153 NTBrowser[3535:157990] CoreData: sql: SELECT Z_VERSION, Z_UUID, Z_PLIST FROM Z_METADAT

Reid
  • 1,109
  • 1
  • 12
  • 27
  • Please check this answer https://stackoverflow.com/a/33379649/5093900 In my case the reason was "attempting to save objects that don't have sufficient fields filled out". – phantom_2 Jun 03 '20 at 15:30

1 Answers1

2

I would recommend to start with enabling SQL debug output. XCode4 and Core Data: How to enable SQL Debugging probably no changes were made.

Community
  • 1
  • 1
heximal
  • 10,327
  • 5
  • 46
  • 69
  • Thanks, that is a good suggestion--I'd forgotten about that argument. When I turn on SQL debugging, I get a few lines of "setup" output. After that, though, neither my fetches nor (attempted) saves produce any additional output (curiously). – Reid Mar 23 '15 at 20:38
  • Actually, now I *am* getting SQL output when I fetch. Everything seems OK there, which I'd expect as it seems OK on screen as well...but I don't get any SQL output when I attempt to save. My guess is that Core Data is balking without even making any SQL statements? – Reid Mar 23 '15 at 20:56
  • exactly. when you change managed object attributes values, it affects only objects in memory. actual data saving by executing multiple insert, update and delete statements happens only when you call save method. the question is why no changes are applying when you call it. – heximal Mar 23 '15 at 21:01
  • I don't see how it thinks it has nothing to save, though. The context's "insertedObjects" property contains my objects. – Reid Mar 23 '15 at 21:17
  • Added SQL debug output to my original question. – Reid Mar 23 '15 at 21:22