swift - Realm-iOS: Model object becomes nil when it's added to realm -


it's rss reader app. instantiate model object, call setupfavicon() on download favicon nil when creating object. add each object realm. when icon fetched, self nil, can't update app's ui. don't understand why self becomes nil.

class article: object {   dynamic var source = ""   dynamic var title = ""   dynamic var link = ""   dynamic var pubdate = date()   dynamic var favicon: data?    dynamic var favicondidload: (() -> ())?    func setupfavicon(_ source: string) {     downloadmanager.sharedinstance.downloadfavicon(source) { [weak self] icon in       if let icon = icon {         self?.favicon = icon         self?.favicondidload?()       }     }   }    override class func primarykey() -> string? {     return "link"   }    override class func ignoredproperties() -> [string] {     return ["favicondidload"]   } } 

closure favicondidload defined in tableviewcell class , invoked when favicon downloaded.

  fileprivate func setupfavicon(_ article: article) {     if let favicon = article.favicon {       setfavicon(favicon)     } else {       article.favicondidload = { [weak self] in         self?.setfavicon(article.favicon)       }     }   } 

you should keep strong reference article objects. when load them realm need keep them in array. if operate on results realm released when method tableviewcell setupfavicon ends.


Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -