为什么没有创建数据库文件且Core Data中没有进行任何操作?

时间:2013-09-20 19:25:05

标签: ios objective-c sqlite core-data

我是ios的新手。我正在努力学习ios,但直到现在我还没有成功在ios中做CRUD。

我正在按照教程进行操作,代码中没有问题,您将看到下面的代码。

  1. 我带有核心数据和ARC的空白应用程序。
  2. 制作一个名为Person的实体(热门教程示例:www.youtube.com/watch?v = bC3F8a4F_KE)
  3. 然后添加了名为IMSPerson的NSObject子类。
  4. 然后添加了UIVIEWCONTROLLER的子类。
  5. 然后在btnSavePerson上创建一个插入方法。
  6. 主要问题是我没有CORE DATA db文件告诉我正在插入数据......

    - (NSPersistentStoreCoordinator *)persistentStoreCoordinator
    {
        if (_persistentStoreCoordinator != nil) {
            return _persistentStoreCoordinator;
        }
    
        NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"CrudWithInterface.sqlite"];
    

    在哪里找到此文件 CrudWithInterface.sqlite

    保存按钮事件

    - (IBAction)btnAddPerson:(id)sender {
        NSEntityDescription *entityDesc = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:context];
        NSManagedObject *newPerson = [[NSManagedObject alloc]initWithEntity:entityDesc insertIntoManagedObjectContext:context];
        [newPerson setValue:self.txtFirstName.text forKey:@"firstName"];
        [newPerson setValue:self.txtFirstName.text forKey:@"lastName"];
        [newPerson setValue:self.txtFirstName.text forKey:@"address"];
    
        NSError *error;
        [context save:&error];
    
        self.fldDisplayData.text = @"person Added Successfully... :)";
    
    }
    

    我也搜索了路径〜/ User / Library / SharedAllication .....

    那里没有IOS模拟器文件夹......

1 个答案:

答案 0 :(得分:0)

试试这个:

步骤1:我没有看到持久性存储协调器的所有代码。如果您想要多次运行应用程序,则必须阅读或创建NSPersistentStoreCoordinator。

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
    if (_persistentStoreCoordinator != nil) {
        return _persistentStoreCoordinator;
    }

    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"CrudWithInterface.sqlite"];

    NSError *error = nil;
    _persistentStoreCoordinator = [[[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:<your managed Obbject Model>] retain];

    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];

    if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error])
    {
        NSLog(@"Suppression du model de données !!");
        [[NSFileManager defaultManager] removeItemAtURL:storeURL error:NULL];

        [_persistentStoreCoordinator release];
        _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:<your managed Obbject Model>];
        if (![singleton addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error])
        {
            abort();
        }
    }

    return _persistentStoreCoordinator;

}

步骤2:现在保存按钮(我现在不知道如何创建您的managedObjectContext命名上下文) 你可以使用静态方法“NSEntityDescription insertNewObjectForEntityForName”它更有用

    - (IBAction)btnAddPerson:(id)sender {

        //Normaly you subclass NSManagedObject. On your modele file Editor menu "Create NSManaged subclass" 

        NSManagedObject *newPerson = [NSEntityDescription insertNewObjectForEntityForName:@"Person"                                                       
inManagedObjectContext: context];
        [newPerson setValue:self.txtFirstName.text forKey:@"firstName"];
        [newPerson setValue:self.txtFirstName.text forKey:@"lastName"];
        [newPerson setValue:self.txtFirstName.text forKey:@"address"];

        NSError *error;
        if(![context save:&error])
        {
            DLog(@"Whoops, couldn't save: %@", [err localizedDescription]);
        }

        self.fldDisplayData.text = @"person Added Successfully... :)";


    }