importing sqlite to coredata

前端 未结 3 814
庸人自扰
庸人自扰 2021-02-11 09:05

I have imported sqlite prepopulated dbs to my coredata projects before, but now I have created a project in the 3.2.5. xcode, wich changes nsurl for nstring in the AppDelegate,

3条回答
  •  再見小時候
    2021-02-11 09:30

    thanks Ishu, you helped me a lot by pointing the NSArray way!!!

    this was the solution I ended up by enlightenment !

    - (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
    
    if (persistentStoreCoordinator_ != nil) {
        return persistentStoreCoordinator_;
    }
    
    
    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"ChildCare_v02.sqlite"];
    
    NSString *storePath = [[NSBundle mainBundle] pathForResource:@"ChildCare_v02" ofType:@"sqlite"];
    
    
    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"ChildCare_v02.sqlite"]; //este es el que sirve!!! CREE ESTE
    
    NSLog(@"store URL %@", storeURL);
    
        // Put down default db if it doesn't already exist
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if (![fileManager fileExistsAtPath:writableDBPath]) {
        NSLog(@"proceda aqui");
        NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:@"ChildCare_v02" ofType:@"sqlite"];
        NSLog(@"no existe todavia");
        NSLog(@"defalultStorePath %@", defaultStorePath);
    
    
        if (defaultStorePath) {
          [fileManager copyItemAtPath:defaultStorePath toPath:writableDBPath error:NULL];
            NSLog(@"storePath= %@", storePath);
        }
    }    
    
    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();
        }    
    
       return persistentStoreCoordinator_;
        }
    

    hope it helps some noob like me wanting to import prepopulated sqlite to a coredata db, note that first need to create blank db, to keep the structure and then populated (I used SQLite database browser to import CSV columns to my sqlite)

    ss9

提交回复
热议问题