1

I'm quite new to OSX and xCode development so any help here appreciated. I'm developing an OSX app that stores data in core data and I want to clear the core data model so that it gets re-generated without having to do a migration as I've not pushed the app to any devices yet. I'm incrementally playing around and adding entities as well as changing attributes/etc on existing entities.

I get this error when running the app via xcode after I've added an attribute to an already created entity:

The managed object model version used to open the persistent store is incompatible with the one that was used to create the persistent store.

I've looked at these questions and answers and I'm not getting them to work:

Deleting CoreData store on OS X?

How do I overcome XCode object model version error?

The answer that seems most likely to resolve this (which doesn't work for me) is to go to ~/Library/Developer/Xcode/DerivedData and delete the application folder there. The DerivedData folder does hold a folder with the name of my application and it does get re-generated when I delete it and have the project open in xCode. In fact I've removed all folders in that DerivedData folder but still the same issue.

Anyone knows how to solve this on OSX xcode 8.1 running El Capitan? My App's deployment target is macOS 10.11.

Community
  • 1
  • 1
jonolo
  • 31
  • 8
  • The persistant store exists only on the target device. Simply delete the app from your device/simulator/macbook (please add tag for target. iOS, macOS...) – shallowThought Dec 08 '16 at 19:34
  • Thanks. I've added some tags. As I'm new to asking on stackoverflow it would really help me if you let me know if my question/tags still doesn't make sense. – jonolo Dec 08 '16 at 21:48

2 Answers2

1

Assuming this is not a document-based app, the Core Data persistent store file will be located in ~/Library/Containers/[your bundle ID]/Data/Documents/. You can remove the full container or specific files. For a document-based app, of course, it'd be in the document.

On iOS you'd just delete the app, but on OS X it's a little different.

Tom Harrington
  • 69,312
  • 10
  • 146
  • 170
  • Thanks. Though I can't find anything within that ~/Library/Containers/ folder that relate to my app :( My bundle identifier starts with 'co.uk....' and I can't find it. I've added some more tags to the question for my setup to give more info. – jonolo Dec 08 '16 at 21:51
  • I assumed you were using sandboxing. If you aren't, then the persistent store is literally wherever your code decided to put it. That might mean in `~/Library/Application Support/` in a directory for your app. – Tom Harrington Dec 08 '16 at 21:59
  • Ok I'm really showing my level of novice here since I didn't know sandboxing existed. I'm reading up on that now. Can't find anything in '~/Library/Application Support/' either – jonolo Dec 08 '16 at 22:10
  • Then look at your code that sets up the Core Data stack. If you're not using `NSPersistentContainer`, you literally have to tell Core Data where the file goes. If you are using `NSPersistentStoreContainer`, you can ask it for its `persistentStoreCoordinator`, ask that for its `persistentStores`, and each of them has a `url` that says where the file is. – Tom Harrington Dec 08 '16 at 22:13
  • Ok what works is enabling sandboxing. Then I find my app folder that I can delete in `~/Library/Containers/` and I can keep changing my core data model. – jonolo Dec 08 '16 at 22:15
  • Great - I printed out the urls from the persistentstores and it ended up showing `~/Library/Application Support/com.apple.toolsQA.CocoaApp_CD/TableViewPlayGround.storedata` (TableViewPlayGround is the name of my app). Deleting that storedata worked!!! Many thanks Tom. Let me know how you want me to write up the answer so you can post and get credit for the help. – jonolo Dec 08 '16 at 22:31
1

Based on comments etc, I'm sharing two ways I've found that solves this issue for me.

Option 1 - adding app sandbox capability.

Add the capability of 'app sandbox' to your app. The data store then gets moved to the ~/Library/Containers/<appName>. When you want to regenerate the core data store to get around versioning issues, you can then delete that folder.

Option 2 - find out the core data persistent store location via code and then delete those files.

As per Tom Harrington: My app used an NSPersistentStoreCoordinator as U could see in my AppDelegate's core data setup. I got the urls of the persistent stores via this in the AppDelegate.swift (I added this just before where the crash happens when the core data versioning error occurs):

for store in (coordinator?.persistentStores)! 
    print("store url: \(store.url)")
}

This showed that the location of the persistent story for my app was ~/Library/Application Support/com.apple.toolsQA.CocoaApp_CD/TableViewPlayGround.st‌​oredata. FYI: My app bundle id is co.uk.gamba.TableViewPlayGround so this means the data store is not kept in folders relating to my app's bundle when running via xcode. The core data model got replaced without error each time I deleted that file and restarted the app in xcode.

jonolo
  • 31
  • 8