开发者

Core Data and MySQL

How would I create a core-data application that syncs with a MySQL database?

Should I implement a SQL-Lite layer and try to sync with MySQL that way?

Or would running web services be better? I want to take advantage of Core Data mod开发者_如何学JAVAeling though.

Is there any way to use Linq? I love linq.


I'm a PHP/MySQL programmer in my day job, and the easiest way I could think of to get data out of MySQL for my Core Data app was to make a HTTP connection to a PHP web server which returned the data in plist xml format. Then I can easily fill an NSArray using the plist data.

For example, this was how I did it in my app:

NSURL *url = [NSURL URLWithString:@"http://myphpwebsite.com/products"];
NSArray *products = [[NSArray alloc] initWithContentsOfURL:url];

Then I had an NSArray of products that I used in my app and persisted to Core Data.

I always recommend that people use PHP, Apache and MySQL together. They work great together on the backend, almost seamlessly. For example, I can easily get data out of MySQL using PHP. Then I can manipulate that data in PHP into the plist xml format used by my iPhone app.


If you are using CoreData you do not directly accessing its underlying backing store (with the exception of implementing custom atomic stores). If you want to sync with MySQL you need to access the MySQL database (either directly via a libmysql or through an app server) and then marshall that data into your managed objects.

In other words, your CoreData model is completely local and you only should access it via the CoreData APIs. How you get data in and out is a completely seperate issue, and the fact that the underlying storage may be sqlite has no bearing on that.

And no, there is no way to use LINQ on the iPhone. I suppose at some point in the future it might be possible through MonoTouch, but unless you are writing a full MonoTouch app I suspect the marshaling objects back and forth to use it would be very unnatural with Objective-C code.


I have code for synchronizing core data with any database on the backend. It uses a web service and does communication via JSON. Not only is it database agnostic but it doesn't care if the data structures are different.

It will be included in the next version of the QuickConnectFamily framework. http://www.quickconnectfamily.org

Let me know if you need the code before the release.a


Also take a look at Restkit.org I was pointed to it when I asked a similar question (but I'm using Core Data), and it looks very promising !

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜