Is it safe to delete old CoreData xcdatamodel versions? - core-data-migration

In my project I have too many CoreData model versions (two-digit number). It's a years-long project and stuff has been changed quite a bit through time.
If I'm 100% sure all my users are using the latest model for quite some time now, is there a way I can get rid of old models and 'start fresh' without pulling all the old versions?
I know of solutions where xcdatamodel entries can manually be removed from project file and from the file system, but is it safe? I don't want to end up having "The model used to open the store is incompatible with the one used to create the store" exception.

Related

Extension always wants to migrate core data store and alway fails

I am trying to write a thumbnail provider extension (on macOS) that accesses the Core Data store of the main application, shared via an app group, to find images to base icon thumbnails on. This worked fine until I added one attribute to the Core Data model. Now the extension always crashes with An error occurred during persistent store migration and attempt to write a readonly database.
Creating a new default model version and deleting the Core Data Store doesn't help. Somehow the thumbnailer extension still thinks that the store written by the main application uses an earlier model, which makes no sense.
If I set shouldMigrateStoreAutomatically to NO, I get The managed object model version used to open the persistent store is incompatible with the one that was used to create the persistent store.
If I set readOnly = YES on the NSPersistentStoreDescription, I get The file couldn’t be saved because you don’t have permission. which I think may be a concurrency isse, caused by lots of thumbnail provider threads trying to migrate the store all at once.
EDIT: Original code here: https://github.com/angstsmurf/spatterlight/blob/quicklook/SpatterlightThumbnails/ThumbnailProvider.m
Right, I got this to work, but I'm not sure how.
Some of the things I did: cleaned up the build folder, deleted all copies of the main application from my hdd, built a new release version of it, put it in Applications, picked that one when Xcode asked me to choose an app to run after building the thumbnail provider extension. That still didn't work, but afterwards I no longer got any store migration errors when building and running in Xcode.
So basically the problem seems to have been that Xcode picked an old build of the main application to run along with the extension, which created an old version of the Core Data store. Or the old store was still being cached somewhere. Just cleaning the build folder was not enough to fix this.

Xcode Core Data models are missing

I am working on an iOS app using Core Data, and after doing some stuff in the storyboard, I tried to run the app and realize that all of my Core Data models are missing except my original one. My playground is also missing, but I don't really care about that. I have no idea what happened, and really need to get those data models back. This is what I see when I look for them in the project navigator.
When I try to build the app, it builds fine, but then I immediatly get this error:
016-02-12 11:46:22.779 App Name[51990:1666336] CoreData: Failed to load NSManagedObjectModel with URL
'file:///Users/myname/Library/Developer/CoreSimulator/Devices/EC2BDB71-BE0C-46CB-B208-09C5D0FD2F3B/data/Containers/Bundle/Application/76FAF1C3-8F85-4A1A-81DE-309B984176E1/App%20Name.app/DataModel.momd/'
fatal error: unexpectedly found nil while unwrapping an Optional value
I tried undoing all my changes I made in git since the last commit, but it still can't find them. What could have happened? Is there any way I can get them back?
It's hard to say exactly what happened, but something is causing your files to disappear. It's not in any way related to Core Data or Swift playgrounds-- you've got a serious problem somewhere and it's making you lose data.
These files should be in your git repository. If you can't find them by undoing changes since your last commit, you need to dig deeper. If they were ever committed, they're recoverable, but going back to the last commit may not be enough. They should also exist in whatever system you use to back up your hard drive.
Core Data may be able to help you recover the models by other means. If you have an older compiled copy of the app somewhere, it will contain compiled versions of the data model files. Xcode can import those and re-create the old model files. You'd do this by creating a new model file, then going to the "Edit" menu and selecting "Import...".
But, all this is secondary to your real problem, which is that you're losing files and you don't know why. You could lose more files in the future. Whatever happened could happen again. You need to figure out why you're losing files and fix that problem.

Core Data hoses itself

Periodically I launch my app from Xcode to find Core Data errors due to an empty model file. The versioned model files are found in the main bundle but the model file still inits to nil. See my answer for partial workarounds but no solution to date.
I've verified that the files exist and are accessible using the access function. I've tried using initWithContentsOfURL and mergedModelFromBundles with the same result.
This happens randomly and I suspect something is changing the model file behind the scenes but I don't know what.
Restarting my computer seems to have solved the security related crash but this also necessitates removing the existing Core Data store file as it complains the existing one doesn't match the model file.
[UPDATE]
Restarting does not solve the problem. But reseting the current version of the Core Data model file did the trick. I changed it to an older version and then back. This question helped me think of this as something to try. Using mergedModelFromBundles: and versioning (CoreData) Still not sure what the root problem is.
[ANOTHER UPDATE]
I put a file monitor on the .xccurrentversion file in my Core Data momd file bundle to observe when the file is being touched. I've noticed that XCode (or some build tool) updates the .xccurrentversion file periodically when XCode comes to foreground (either when the project is opened or when it's been in background for a while). When it did this today it updated the file setting _XCCurrentVersionName to the main momd file bundle rather than the version file. I replaced the .xccurrentversion file with an older version and it launched. This would explain why reseting the current model version fixes this issue. I have no idea what the conditions are for XCode to update this file.

How do I Split Application into a new Version for a different User Base?

We have a website application that stores data and pictures for a specific customer. We are about to release the same application for use by another customer. The second application will eventually be customized for the second customer. Eventually we hope to have several customers using their own versions of the application.
We are using ASP.NET in Visual Studio 2012. Should we:
clone the existing application and maintain separate code bases?
add a project to the existing solution for the new customer?
We have searched for an answer to but this seems to be a rare situation.
Thanks.
I dont think its rare at all. SAP and Maximo use this a a businiess model. Same core but each package customized to the clients specifications. I have done this (on a much much smaller scale) with some of the programs that we have.
We always start a new project rather than just copy the old. No telling what is in the old one that references the old client. Sort of embarasing when an About window that you forgot about is for someone elses company.
All the code, forms, reports that are customizeable should be in the project for that customer. All of the code, forms, reports that are standard should be in a library.
It really depends on the scope of the application. I've had to do this internally with the company I'm working for; I wrote one solution for one company, then the sister company found out and wanted the same and had to implement it there.
I had a fairly small project to work on, so it was easy to make it universal (while also keeping things rooting from the same code base). All i did was:
break out the unique setting [page title?] using appSettings or similar.
add a new configuration to your solution. Then take advantage of the *.config migrations to:
set connectionStrings
specify appSettings values
When it comes to unique business logic, I had the luxury of using the *.config migrations (most of the data I gathered came from WCF endpoints of services local to the company)--so I lucked out. However, you could make generic interfaces within the app then break out implementation for each company in to separate libraries.

Codekit: Any way to reorder projects?

Working with codekit at the office, my project list is getting somewhat extensive. We have two or three network drives that we do work on, and whenever one isn't connected I get scattered 'X'es in my project list - as a compulsive organizer, it sends my skin crawling.
Is there any way to automatically sort projects, even just by name?
I considered removing and re-adding projects in the order I want them to appear, but then I'd have to remove a good chunk of the list to put a new project in proper order (and I open a new project sometimes once a week). Any insight is appreciated - thanks!
Coming in 2.0. (I would know; I write the app.)

Resources