开发者

Core data storage is repeated

I am trying to use Core Data in my application and I have been succesful in storing data into the entity.The data storage is done in the applicationDidFinishLaunchingWithOptions() method.But when I run the app again,it again gets saved.So how do I check if the data is already present or not??

Here is the code(Saving):

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:&erro开发者_StackOverflowr]) {
    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"]);
}

` Another thing I want to know that if I have thousands of records to store,then is there any other way to do it or can it be done through coding only???

Thanks


  1. from what i understand you want to add all the data once only one time?

if so, move the inserting to the persistentStoreCoordinator method, and check if this is the first time the app lunches, by checking :

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

if it does then load the data. if not do nothing. this is how it look's at the end :

- (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 way i do that is to create a plist with all the data. b.import the plist as array of dictionaries (each dictionary is an entity". c. set a function that iterates throw the array and adds the entities to the context.

something like that:

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
    }

}

I have tried to simplify the answer, but you should know there are alot of thing you can do to better the process.

good luck

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜