Search code examples
iosswiftuicollectionviewnsfetchedresultscontroller

Updating uicollectionview when core data object changes


I'm trying to convert a table view to a collection view. I have it display the cells fine but any changes made to them aren't being displayed until i leave the view/come back or scroll the cell off screen. The data comes from core data

The first cell is being update correctly, but the other cells aren't. If i update again, they get updated but only to the value of the previous update. I can see the update function being called but the configure cell isn't being called to actually update the contents, only on the first cell

Full code here: http://pastebin.com/BDaVS6vq

The results of the nslogs give

// View initial load 2016-05-18 15:03:38.012 Calona[1304:315892] 3. Pet: Optional("Pea") - Exp: Optional(2000) 2016-05-18 15:03:38.022 Calona[1304:315892] 3. Pet: Optional("Squi") - Exp: Optional(2000) 2016-05-18 15:03:38.033 Calona[1304:315892] 3. Pet: Optional("Whisper Light") - Exp: Optional(2000)

// After first update 2016-05-18 15:06:12.686 Calona[1309:316804] 1. User Pet: Optional("Pea") 2016-05-18 15:06:12.698 Calona[1309:316804] 2. Update 2016-05-18 15:06:12.705 Calona[1309:316804] 3. Pet: Optional("Pea") - Exp: Optional(4000) 2016-05-18 15:06:12.710 Calona[1309:316804] 3. Pet: Optional("Squi") - Exp: Optional(3000) 2016-05-18 15:06:12.714 Calona[1309:316804] 3. Pet: Optional("Whisper Light") - Exp: Optional(3000) 2016-05-18 15:06:12.725 Calona[1309:316804] 1. User Pet: Optional("Squi") 2016-05-18 15:06:12.732 Calona[1309:316804] 2. Update 2016-05-18 15:06:12.737 Calona[1309:316804] 1. User Pet: Optional("Whisper Light") 2016-05-18 15:06:12.742 Calona[1309:316804] 2. Update 2016-05-18 15:06:12.895 Calona[1309:316804] 2. Update 2016-05-18 15:06:12.895 Calona[1309:316804] 2. Update

After second update 2016-05-18 15:06:26.903 Calona[1309:316804] 1. User Pet: Optional("Pea") 2016-05-18 15:06:26.918 Calona[1309:316804] 2. Update 2016-05-18 15:06:26.924 Calona[1309:316804] 3. Pet: Optional("Pea") - Exp: Optional(5000) 2016-05-18 15:06:26.931 Calona[1309:316804] 3. Pet: Optional("Squi") - Exp: Optional(4000) 2016-05-18 15:06:26.936 Calona[1309:316804] 3. Pet: Optional("Whisper Light") - Exp: Optional(4000) 2016-05-18 15:06:26.945 Calona[1309:316804] 1. User Pet: Optional("Squi") 2016-05-18 15:06:26.952 Calona[1309:316804] 2. Update 2016-05-18 15:06:26.958 Calona[1309:316804] 1. User Pet: Optional("Whisper Light") 2016-05-18 15:06:26.962 Calona[1309:316804] 2. Update 2016-05-18 15:06:27.045 Calona[1309:316804] 2. Update 2016-05-18 15:06:27.047 Calona[1309:316804] 2. Update 2016-05-18 15:06:27.048 Calona[1309:316804] 2. Update


Solution

  • used the code available here and its working

    https://gist.githubusercontent.com/jonzhan/e821b7c7fbd13f736768/raw/b84db699779a383506704ecc92814a185502a69f/UICollection%2520and%2520NSFetchedResultsControllerDelegate%2520integration.swift