Currently, I have the following core data backed collection view, which enable users to perform add/ delete/ modify/ reordering.
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?
Use strong
Or, such concern is invalid. We can simply use strong
class TabInfoSettingsCell: UICollectionViewCell {
// NSManagedObject.
private var nsTabInfo : NSTabInfo?
Use struct
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.
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?