Core data 赢得核心数据';t在文档中创建DataStore.sqlite

Core data 赢得核心数据';t在文档中创建DataStore.sqlite,core-data,ios-simulator,xcode4.5,Core Data,Ios Simulator,Xcode4.5,我希望核心数据在Mac的Documents目录中创建一个DataStore.sqlite DB,但由于某些原因,它没有。为什么? 以下是AppDelegate.h和.m中的代码 AppDelegate.h #import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window;

我希望核心数据在Mac的Documents目录中创建一个DataStore.sqlite DB,但由于某些原因,它没有。为什么?

以下是AppDelegate.h和.m中的代码

AppDelegate.h

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@end
#导入
@接口AppDelegate:UIResponder
@属性(强,非原子)UIWindow*window;
@结束
AppDelegate.m

#import "AppDelegate.h"
#import <CoreData/CoreData.h>
#import "CurrentLocationViewController.h"

@interface AppDelegate()

@property (nonatomic, strong) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, strong) NSManagedObjectModel *managedObjectModel;
@property (nonatomic, strong) NSPersistentStoreCoordinator *persistentStoreCoordinator;

@end

@implementation AppDelegate

@synthesize managedObjectContext, managedObjectModel, persistentStoreCoordinator;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;

    UINavigationController *navigationController = (UINavigationController *)[[tabBarController viewControllers]
    objectAtIndex:0];
    CurrentLocationViewController *currentLocationViewController = (CurrentLocationViewController *)
    [[navigationController viewControllers] objectAtIndex:0];
    currentLocationViewController.managedObjectContext = self.managedObjectContext;

    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

#pragma mark - Core Data

- (NSManagedObjectModel *)managedObjectModel
{
    if (managedObjectModel == nil) {
        NSString *modelPath = [[NSBundle mainBundle] pathForResource:@"DataModel" ofType:@"momd"];
        NSURL *modelURL = [NSURL fileURLWithPath:modelPath];
        managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
    }
    return managedObjectModel;
}

- (NSString *)documentsDirectory
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    return documentsDirectory;
}

- (NSString *)dataStorePath
{
    return [[self documentsDirectory] stringByAppendingPathComponent:@"DataStore.sqlite"];
}

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
    if (persistentStoreCoordinator == nil) {
        NSURL *storeURL = [NSURL fileURLWithPath:[self dataStorePath]];

        persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:self.managedObjectModel];

        NSError *error;
        if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
            NSLog(@"Error adding persistent store %@, %@", error, [error userInfo]);
            abort();
        }
    }
    return persistentStoreCoordinator;
}

- (NSManagedObjectContext *)managedObjectContext
{
    if (managedObjectContext == nil) {
        NSPersistentStoreCoordinator *coordinator = self.persistentStoreCoordinator;
        if (coordinator != nil) {
            managedObjectContext = [[NSManagedObjectContext alloc] init];
            [managedObjectContext setPersistentStoreCoordinator:coordinator];
        }
    }
    return managedObjectContext;
}


@end
#导入“AppDelegate.h”
#进口
#导入“CurrentLocationViewController.h”
@接口AppDelegate()
@属性(非原子,强)NSManagedObjectContext*managedObjectContext;
@属性(非原子,强)NSManagedObjectModel*managedObjectModel;
@属性(非原子,强)NSPersistentStoreCoordinator*persistentStoreCoordinator;
@结束
@实现AppDelegate
@综合managedObjectContext、managedObjectModel、persistentStoreCoordinator;
-(BOOL)应用程序:(UIApplication*)应用程序使用选项完成启动:(NSDictionary*)启动选项
{
UITabBarController*tabBarController=(UITabBarController*)self.window.rootViewController;
UINavigationController*导航控制器=(UINavigationController*)[[tabBarController视图控制器]
objectAtIndex:0];
CurrentLocationViewController*CurrentLocationViewController=(CurrentLocationViewController*)
[[navigationController ViewController]对象索引:0];
currentLocationViewController.managedObjectContext=self.managedObjectContext;
返回YES;
}
-(无效)应用程序将重新签名:(UIApplication*)应用程序
{
//当应用程序即将从活动状态移动到非活动状态时发送。这可能发生在某些类型的临时中断(如来电或短信)或用户退出应用程序并开始转换到后台状态时。
//使用此方法暂停正在进行的任务、禁用计时器和降低OpenGL ES帧速率。游戏应使用此方法暂停游戏。
}
-(无效)应用程序标识符背景:(UIApplication*)应用程序
{
//使用此方法释放共享资源、保存用户数据、使计时器无效,并存储足够的应用程序状态信息,以便在应用程序稍后终止时将其恢复到当前状态。
//如果您的应用程序支持后台执行,则会调用此方法而不是applicationWillTerminate:当用户退出时。
}
-(无效)应用程序将进入前台:(UIApplication*)应用程序
{
//作为从后台转换到非活动状态的一部分调用;在这里,您可以撤消进入后台时所做的许多更改。
}
-(无效)应用IDBECOMEACTIVE:(UIApplication*)应用
{
//重新启动应用程序处于非活动状态时暂停(或尚未启动)的所有任务。如果应用程序以前位于后台,可以选择刷新用户界面。
}
-(无效)申请将终止:(UIApplication*)申请
{
//当应用程序即将终止时调用。如果合适,请保存数据。另请参阅ApplicationIdentinterBackground:。
}
#pragma标记-核心数据
-(NSManagedObjectModel*)managedObjectModel
{
if(managedObjectModel==nil){
NSString*modelPath=[[NSBundle mainBundle]pathForResource:@“数据模型”的类型:@“momd”];
NSURL*modelURL=[NSURL fileURLWithPath:modelPath];
managedObjectModel=[[NSManagedObjectModel alloc]initWithContentsOfURL:modelURL];
}
回归模型;
}
-(NSString*)文档目录
{
NSArray*Path=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,是);
NSString*documentsDirectory=[paths objectAtIndex:0];
返回文件目录;
}
-(NSString*)数据存储路径
{
返回[[self-documentsDirectory]stringByAppendingPathComponent:@“DataStore.sqlite”];
}
-(NSPersistentStoreCoordinator*)persistentStoreCoordinator
{
if(persistentStoreCoordinator==nil){
NSURL*storeURL=[NSURL fileURLWithPath:[自数据存储路径]];
persistentStoreCoordinator=[[NSPersistentStoreCoordinator alloc]initWithManagedObjectModel:self.managedObjectModel];
n错误*错误;
if(![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType配置:nil URL:storeURL选项:nil错误:&错误]){
NSLog(@“添加永久存储时出错%@,%@”,错误,[Error userInfo]);
中止();
}
}
返回persistentStoreCoordinator;
}
-(NSManagedObjectContext*)managedObjectContext
{
if(managedObjectContext==nil){
NSPersistentStoreCoordinator*coordinator=self.persistentStoreCoordinator;
if(协调器!=nil){
managedObjectContext=[[NSManagedObjectContext alloc]init];
[managedObjectContext setPersistentStoreCoordinator:coordinator];
}
}
返回managedObjectContext;
}
@结束

当您尝试添加持久存储时,会收到什么错误消息?这就是创建文件的地方。如果它没有被创建,可能会有一个错误说明原因。有趣的是:我没有收到错误消息。没有错误,没有警告。只是当我转到文档时,我找不到DataStore.sqliteOh,我找到了。它在iOS模拟器文档中,而不是Mac上的文档中。