简体   繁体   中英

Sync iOS / Mac app with dame data model?

I crated an iOS app that uses core data, basically a todo list.

I want to create an OS X app that will also show the todo list and sync with the iOS device. I'm not interested in iCloud atm (sync would use bluetooth/internal network) though it's good if it can be enabled later.

I have currently little idea about OS X development but I found I can use a document based app and core data. But I would have to replicate the xcdatamodel of the iOS app. Is there a way I can share the data model? Do I maybe have to use something different than core data? I don't want to have to maintain 2 exact copies... I also don't want to use 3d party libraries (though advices are welcome as a comment). Probably this can be solved using scripting but I wonder if there's a better way.

So the question was how to avoid replicating your xcdatamodel and have only one versioned xcdatamodel in your iOS or in your OS X XCode project/workspace and share it with the other one?!?

Whenever the model has changed you can use the momc tool and compile the "xcdatamodel" file to a "momd" file. Only the "momd" file will be loaded at runtime by iOS/OS X App. The compile step looks like this:

/Applications/Xcode.app/Contents/Developer/usr/bin/momc <your project folder>/MyApplication/MyApplication/MyApplication.xcdatamodeld/MyApplication.xcdatamodel <your destination folder>/my.momd

Now add "my.momd" as a new bundle resource in XCode project of the app with no model of its own. Choose target settings go to "Build Phases" go to "Copy Bundle Resource" and add "my.momd".

You need to change the code accordingly:

NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"my" withExtension:@"momd"];
_managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];

Of course ".xcdatamodeld" folder can be removed from the app project with no model of its own.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM