重复核心数据存储

时间:2010-12-30 05:46:00

标签: iphone

我正在尝试在我的应用程序中使用Core Data,并且我已经成功地将数据存储到实体中。数据存储在applicationDidFinishLaunchingWithOptions()方法中完成。但是当我再次运行应用程序时,它再次被保存。那么如何检查数据是否已经存在?

这是代码(保存):

NSManagedObjectContext *context = [self managedObjectContext];
NSManagedObject *failedBankInfo = [NSEntityDescription
    insertNewObjectForEntityForName:@"FailedBankInfo" 
    inManagedObjectContext:context];
[failedBankInfo setValue:@"Test Bank" forKey:@"name"];
[failedBankInfo setValue:@"Testville" forKey:@"city"];
[failedBankInfo setValue:@"Testland" forKey:@"state"];

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

(Retrieving):-
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription 
    entityForName:@"FailedBankInfo" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
for (NSManagedObject *info in fetchedObjects) {
    NSLog(@"Name: %@", [info valueForKey:@"name"]);
}

` 另一件事我想知道,如果我有数千条记录存储,那么还有其他方法可以做到这一点,还是只能通过编码来完成???

谢谢

1 个答案:

答案 0 :(得分:0)

  1. 根据我的理解,您只想一次添加所有数据吗?
  2. 如果是这样,请将插入移动到persistentStoreCoordinator方法,并通过检查确认这是否是应用程序第一次午餐:

     if (![[NSFileManager defaultManager] fileExistsAtPath:[storeURL path] isDirectory:NULL]) {
            firstRun = YES;     
        }
    

    如果确实如此,则加载数据。如果什么都不做。这就是它最终的样子:

    - (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
    
        if (persistentStoreCoordinator_ != nil) {
            return persistentStoreCoordinator_;
        }
    
        NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"your_app.sqlite"];
    
        BOOL firstRun = NO; 
        if (![[NSFileManager defaultManager] fileExistsAtPath:[storeURL path] isDirectory:NULL]) {
            firstRun = YES;     
        }
    
        NSError *error = nil;
        persistentStoreCoordinator_ = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
        if (![persistentStoreCoordinator_ addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            abort();
        }    
        if (firstRun) {
    
            NSManagedObject *failedBankInfo = [NSEntityDescription
                 insertNewObjectForEntityForName:@"FailedBankInfo" 
               inManagedObjectContext:context];
              [failedBankInfo setValue:@"Test Bank" forKey:@"name"];
             [failedBankInfo setValue:@"Testville" forKey:@"city"];
             [failedBankInfo setValue:@"Testland" forKey:@"state"];
    
            NSError *error;
            [moc save:&error];
    
        }
        return persistentStoreCoordinator_;
    }
    
    1. 我这样做的方法是创建一个包含所有数据的plist。 b。将plist作为字典数组导入(每个字典都是一个实体)。 C。设置一个迭代的函数抛出数组并将实体添加到上下文中。
    2. 类似的东西:

      NSString *thePath = [[NSBundle mainBundle]  pathForResource:@"recordsList" ofType:@"plist"];
          NSArray *recordsArray = [[NSArray alloc] initWithContentsOfFile:thePath];
      
      
      
          for (int i=0;i<[recordsArray count];i++)
          {
             //add the objects to the context
          }
      

      }

      我试图简化答案,但你应该知道你可以采取很多措施来改善这个过程。

      祝你好运