如何添加SQLite作为CoreData中的持久存储? [英] How to add SQLite as persistent store in CoreData?

查看:52
本文介绍了如何添加SQLite作为CoreData中的持久存储?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

客观上,在使用coredata概念时,我们需要将拆分创建为持久性存储,否则它将自动创建?

In objective while using the the coredata concept we need to create the splite as persistent store or it will created automatically ?

推荐答案

在Xcode中创建新的iPhone应用程序项目时,大多数模板都允许使用核心数据进行存储"选项.选中此复选框后,应用程序代表将包括一些样板核心数据代码,其中包括用于打开和/或创建持久性存储的该块:

When creating a new iPhone app project in Xcode, most of the templates allow the "Use Core Data for storage" option. When this is checked, the application delegate will include some boilerplate Core Data code including this block that opens and/or creates the persistent store:

/**
 Returns the persistent store coordinator for the application.
 If the coordinator doesn't already exist, it is created and the application's store added to it.
 */
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {

    if (persistentStoreCoordinator != nil) {
        return persistentStoreCoordinator;
    }

    NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent:@"App.sqlite"]];

    NSError *error = nil;
    persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
    if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:nil error:&error]) {
        /*
         Replace this implementation with code to handle the error appropriately.

         abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.

         Typical reasons for an error here include:
         * The persistent store is not accessible
         * The schema for the persistent store is incompatible with current managed object model
         Check the error message to determine what the actual problem was.
         */
        //NSLog(@"Error with persistent store %@, %@", error, [error userInfo]);
        NSLog(@"Error with persistent store (did the data model change?)");
        abort();
    }

    return persistentStoreCoordinator;
}

这篇关于如何添加SQLite作为CoreData中的持久存储?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆