2011-06-25 103 views
0
IfIDieAppDelegate.m 

    - (void)createEditableCopyOfDatabaseIfNeeded 

    { 

    // First, test for existence. 
    BOOL success; 

    NSFileManager *fileManager = [NSFileManager defaultManager]; 

    NSError *error; 

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 

    NSString *documentsDirectory = [paths objectAtIndex:0]; 

    //NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"DeathDiary.sqlite"]; 

    NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"IfIDie.sqlite"]; 

    NSLog(@"%@",writableDBPath); 

    success = [fileManager fileExistsAtPath:writableDBPath]; 

    if (success==YES) 

    { 

     NSLog(@"Database Already Exists"); 
     return; 
    } 
    else { 

     NSLog(@"New Database Created"); 

     // The writable database does not exist, so copy the default to the appropriate location. 


    NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"IfIDie.sqlite"]; 

     NSLog(@"Default : %@",defaultDBPath); 

     success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error]; 

     if (!success) { 

      NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]); 

     } 

    } 

} 


- (NSPersistentStoreCoordinator *)persistentStoreCoordinator { 

    if (persistentStoreCoordinator != nil) { 

     return persistentStoreCoordinator; 

    } 

    NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"IfIDie.sqlite"]]; 

    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; 
} 


NoteEditController.m 


- (void)viewWillDisappear:(BOOL)animated{ 

    [super viewWillDisappear:animated]; 

    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidShowNotification object:nil]; 

    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil]; 

    if(saveChanges){ 

     // Save any changes to note 
     if([[noteTextView text] length] > 0){ 

      if(noteToEdit){ 

       // Edit Note 
       if([[titleField text] length] <= 0) 

        [noteToEdit setNoteTitle:[noteTextView text]]; 

       else 

        [noteToEdit setNoteTitle:[titleField text]]; 

       [noteToEdit setNoteText:[noteTextView text]]; 

      } else { 
       // New Note 
       Note *newNote = [NSEntityDescription 
insertNewObjectForEntityForName:@"Note" inManagedObjectContext:context]; 

       if([[titleField text] length] <= 0) 

        [newNote setNoteTitle:[noteTextView text]]; 

       else 

        [newNote setNoteTitle:[titleField text]]; 


       [newNote setNoteText:[noteTextView text]]; 

       NSLog(@"data saved"); 


      } 

     } else { 
      // Remove note (zero length) 

      if(noteToEdit){ 

       [context deleteObject:noteToEdit]; 
      } 
     } 
    } 
} 

在這裏一切似乎都會好起來,但仍然沒有數據保存到表中。有什麼可能是錯誤的? 是否有數據庫重新加載?如果,然後我沒有得到如何解決。 沒有錯誤,顯示已保存但未保存到表的nslog數據。使用核心數據存儲數據到sqlite表的問題

+1

請編輯您的帖子以刪除線之間多餘的空格,並正確指定代碼塊(curlybrace按鈕當代碼突出顯示時爲'{}')。現在,它是不可讀的 – RyanR

回答

0

您需要保存上下文實際提交更改viewWillDisappear:方法結束

NSError *error = nil; 
if (![context save: &error]) { 
    // Couldn't save 
} 
+0

其done.but對不起我沒有得到這個代碼如何提交insertion.can你請解釋一下? –

+1

'context'是文檔中提到的便箋本。所有更改(插入,刪除和編輯)都存儲在上下文中,並且只有在您調用'save:'時纔會寫入數據庫。如果你不這樣做,數據庫不會改變。 –