2015-09-17 46 views
1

關於以下內容的任何建議?幫助非常感謝:NSFetchedResultsController在保存到核心數據時未更新

我已經安裝了NSFetchedResultsController如下:

​​

它是由一個聊天實體一對多的關係聯繫起來。聊天>>消息。一個聊天(由CurrentChatter定義)有很多消息。

當我加載的tableview ...的消息顯示完全用我的ViewDidLoad()如下:

do { 
    try fetchedResultsController.performFetch() 
} catch let fetchError as NSError { 
    print("I have been unable to fetch things .. : \(fetchError)") 
} 

當我使用以下樣板代碼保存的消息,似乎沒有任何與NSFetchedResultsController發生。由於某種原因,tableView沒有更新,我似乎無法解決原因。我有NSFetchedResultsControllerDelegate就位和定義。

if let newMessage = NSEntityDescription.insertNewObjectForEntityForName("Messages", inManagedObjectContext: mOC) as? Messages { 
    newMessage.messageDate = NSDate() 
    newMessage.messageFrom = myMainUser.mainUserIDfromServer 
    newMessage.messageMustBeSent = false 
    newMessage.messageReceivedAtServer = true 
    newMessage.messageCode = 0 
    newMessage.messageType = 100 
    newMessage.messageText = "Here is a message I am sending to you!" 
    newMessage.messageTo = Int(profileID)! 
    newMessage.messageNewMessage = false 

    currentChatter.mutableSetValueForKey("messages").addObject(newMessage)   

    do { 
     try mOC.save() 
    } catch let saveError as NSError { 
     print("I have not been able to save the message ...: \(saveError)") 
    } 
} 

我打電話給mOC.save。我檢查過它已保存,並且保存到相同的ManagedObjectContext。當我手動刷新tableview時,顯示新消息。

爲了測試這個工作我有一個簡單的print語句如下:

func controllerWillChangeContent(controller: NSFetchedResultsController) { 

    print("I will make changes?") 

} 

不用說,這是沒有得到調用。

這是因爲新消息被添加爲我的CurrentChatter的一部分。還是應該每次更新郵件實體時都進行更新?或者我完全錯過了什麼? 3個小時,我不理解爲什麼它不工作。

在此先感謝。

UPDATE:

我添加的代碼按日期排序表(增加供參考):

import Foundation 
import CoreData 

class Messages: NSManagedObject { 

    func sectionTitle() -> String { 

     let myDate = NSDateFormatter() 
     myDate.dateFormat = "d MMM YYYY" 
     return myDate.stringFromDate(self.messageDate) 
    } 

} 
+0

您的控制器中是否有典型的FRC委託方法代碼? – andrewbuilder

回答

2

您設置委託,但你實現controller didChangeObject等?你需要的東西是這樣的:

func controllerWillChangeContent(controller: NSFetchedResultsController) { 
     tableView.beginUpdates() 
    } 

    func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) { 
     switch type { 
     case .Insert: 
      tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: UITableViewRowAnimation.Automatic) 
     case .Delete: 
      tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: UITableViewRowAnimation.Automatic) 
     case .Update 
      // update cell at indexPath 
     case .Move: 
      tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: UITableViewRowAnimation.Automatic) 
      tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: UITableViewRowAnimation.Automatic) 
     } 
    } 

    func controllerDidChangeContent(controller: NSFetchedResultsController) { 
     tableView.endUpdates() 
    } 
+0

嗨。感謝您的答覆!我錯過了一個func()聲明,我更新了這個問題。我有一個樣板打印語句來檢查'controllerWillChangeContent'是否被調用。它並沒有被調用。 – Darren

+0

您是否嘗試將'sectionNameKeyPath'設置爲零? – MirekE

+0

是的。沒有不同。同樣的行爲。當我添加一條「消息」時,沒有任何內容出現,直到我手動重新加載tableview。 – Darren

0

如果將UPDATE語句.Insert在didChangeObject(所以把它放在第1名)之前,有何幫助?我有同樣的行爲,併爲我修復它。至少,在我的設備上。我現在聽到beta測試者遇到同樣的問題.. :(

+0

不,沒有什麼區別。當我查看「類型」值時,它沒有顯示正確的操作。所以,當我'更新'一條記錄時,'type'結果在'didChangeObject'函數中作爲'delete'返回。這真的很煩人,因爲在使用'NSFetchedResultsControllerDelegate'時,我現在不得不在每次進行更改時手動重新加載tableView來使用它。儘管感謝您的建議。我似乎並不是唯一有這個問題的人。 – Darren

1

我有一個類似的問題。在我的情況下,問題是我已經設置了NSFRC委託的類型變量實例化之前從NSFetchedResultsController()調用的結果因此該委託沒有被設定並沒有委託方法是可行的。

愚蠢的錯誤,我知道,但我花了一段時間來找到它。

在你的情況,如果你找回東西爲typedidChangeObject那麼你的代表工作正常。

你說你得到了錯誤的值type。打印type枚舉的原始值以查看它們是什麼。下面是蘋果的文件連結他們:

https://developer.apple.com/library/prerelease/ios/documentation/CoreData/Reference/NSFetchedResultsControllerDelegate_Protocol/index.html#//apple_ref/c/tdef/NSFetchedResultsChangeType

他們在switch聲明順序應該不是隻要事情,因爲它是廣泛(包括他們全部)。

此外,請確保您不期待一種類型和另一種類型。例如:

如果添加新條目,您會期望.Insert正確嗎?但是,如果您在NSFRC描述符中排序爲name,則會收到.Move,因爲新添加可能導致行重新排列。

這也發生在我身上,並花了我一段時間才發現。

希望這會有所幫助。