2010-12-30 15 views
0

私のアプリケーションでコアデータを使用しようとしていて、エンティティにデータを格納するのに成功しました。データ格納は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"]); 
} 

` もう一つは:ここで

コード(保存)ですコーディングのみで済む???

おかげ

答えて

0
  1. 私はあなたが唯一の1時間後にすべてのデータを追加したい理解して何から?

もしそうであれば、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. 私はそれを行うa.the方法は、すべてのデータとのplistを作成することです:これは終わりのを見てどのようです。辞書の配列としてのplist(各辞書は、「実体であるb.import C反復するが、アレイをスローし、コンテキストにエンティティを追加する機能を設定しているよう

何か:。。。

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 
    } 

}

私は答えを簡素化しようとしましたが、あなたは、プロセスをより良くするために何ができる事がたくさんあります知っている必要があります。

行きますお幸運

+0

ありがとう!それは本当に働いた.. – Kamlesh

関連する問題