What is the difference between .sqlite and .stored

2020-03-04 07:26发布

When you start a new iOS project on Xcode using core data, it initializes a database with the extension .sqlite. When you do the same thing for a new project for OSX the database has the extension .storedata.

Is there any difference between the two? thanks.

1条回答
家丑人穷心不美
2楼-- · 2020-03-04 08:14

CoreData on iOS only supports an sqlite persistent store. CoreData on OS X supports multiple formats including sqlite and xml, with the default persistent store being xml-based. Thus .sqlite is an sqlite persistent store for CoreData, whereas .storedata is an xml persistent store.

To expand on the answer, an sqlite persistent store allows the model to be partially and incrementally loaded, whereas an xml persistent store only allows (requires) the model to be loaded en masse. The difference in defaults is probably explained by the differing memory availability on the two platforms. With much more memory available on a typical Mac, the overall performance is enhanced by loading everything at once.

To switch the default code to use sqlite instead of xml, edit persistentStoreCoordinator and change:

NSURL *url = [applicationFilesDirectory URLByAppendingPathComponent:@"Foo.storedata"];
NSPersistentStoreCoordinator *coordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:mom];
if (![coordinator addPersistentStoreWithType:NSXMLStoreType configuration:nil URL:url options:nil error:&error]) {

to:

NSURL *url = [applicationFilesDirectory URLByAppendingPathComponent:@"Foo.sqlite"];
NSPersistentStoreCoordinator *coordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:mom];
if (![coordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:url options:nil error:&error]) {
查看更多
登录 后发表回答