Cheok Yan Cheng
Cheok Yan Cheng

Reputation: 42768

Should I use weak to let UICollectionViewCell hold an NSManagedObject, to avoid from interferencing with CoreData memory management?

Currently, I have the following core data backed collection view, which enable users to perform add/ delete/ modify/ reordering.

enter image description here

It will be much more convenient to achieve what I, if I let UICollectionViewCell, to hold its corresponding NSManagedObject. So that I can each perform add/ delete/ modify/ reordering.

I was wondering, should I mark the NSManagedObject as weak, to avoid from interferencing with how CoreData manage its memory?


Using weak

class TabInfoSettingsCell: UICollectionViewCell {
    // NSManagedObject. Use weak, as we do not want to inteference with how CoreData manage its memory.
    private weak var nsTabInfo : NSTabInfo?

Or, such concern is invalid. We can simply use strong

Use strong

class TabInfoSettingsCell: UICollectionViewCell {
    // NSManagedObject.
    private var nsTabInfo : NSTabInfo?

Or, such concern is valid and using weak is unsafe (as it can become nil in some edge case?). We can use a struct, which contains everything including objectID, but excluding NSManagedObjectContext.

Use struct

class TabInfoSettingsCell: UICollectionViewCell {
    // struct. Contains everything including objectID, but excluding NSManagedObjectContext.
    private var tabInfo : TabInfo?

This come with the cost of having to create a struct out from NSManagedObject each time, in cellForItemAt.

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    ...
    tabInfoSettingsCell.update(nsTabInfo.toTabInfo())
    ...
}

May I know, which is the correct design, as far as safe memory management is concerned?

Upvotes: 1

Views: 128

Answers (2)

Warren Burton
Warren Burton

Reputation: 17381

You can use a strong reference to your managed object. You are not creating a circular reference which would be a reason to use weak.

e.g this would be bad:

cell.tabInfo = managedObject
managedObject.cell = cell

Also a style point, namespacing your classes with NS leads to confusing your object with Apple classes and should be avoided.

Upvotes: 1

Tarun Tyagi
Tarun Tyagi

Reputation: 10112

When you are using NSFetchedResultsController backed UICollectionView, all of objects lifecycle is driven by NSFetchedResultsController without you ever needing to worry about these cases.

Every time a change happens in your store, you are notified of that change and you can ask NSFetchedResultsController to provide you the latest data.

You don't need to worry too much about a cell holding a strong reference to the model. Cells are reused anyway and this reference will keep changing every time a cell is reused.

You can use strong references to your models in the cells and it won't create issues for you because the cells are refreshed with latest data by design.

Upvotes: 1

Related Questions