ios - Instance variable NSArray not updated correctly in Swift 2 - xCode 7.2 -
my message object has to, , content variable. i'm creating page display tableview unique conversation history. meaning names , profile pictures of people exchanged messages with. made nsarray variable append data in loaddata function display them on table. parse query returns 2 objects, when print resultdata array count prints 0 , reloaddata of course doesn't work. however, adding print statement line after append result object array in loaddata() function, works , prints correctly twice means nsarray not updating. knows why?
this code :
import foundation import uikit import parse class messagescontroller: uiviewcontroller, uitableviewdatasource, uitableviewdelegate{ @iboutlet weak var loadingindicator: uiactivityindicatorview! @iboutlet var tableview: uitableview! var resultdata:nsmutablearray = nsmutablearray() var users = [string]() func loaddata(){ loadingindicator.hidden = false loadingindicator.startanimating() resultdata.removeallobjects() var counter = 0 let user = pfuser.currentuser()?.objectid let checkconversationspredicate = nspredicate(format: "(to = '\(user!)') or (from = '\(user!)')") let checkconversationsquery = pfquery(classname: "messages", predicate: checkconversationspredicate) checkconversationsquery.findobjectsinbackgroundwithblock { (objects, error) -> void in if let objects = objects { object in objects { if (object.valueforkey("to") as? string) == user { //check if in list yet. if not, add him. var isduplicate = false let = object.valueforkey("from") as! string 1 in self.users { if 1 == { isduplicate = true } } //if it's not duplicate add our array of users if !isduplicate { let userquery = pfuser.query() userquery?.wherekey("objectid", equalto: object["from"] as! string) userquery?.findobjectsinbackgroundwithblock({ (users, error ) -> void in if let users = users { user in users{ let followed = user as! pfuser self.resultdata.addobject(followed) self.users.append(followed.objectid!) counter++ } } }) } } else { //check if in list yet, if not add him. var isduplicate = false let = object.valueforkey("to") as! string 1 in self.users { if 1 == { isduplicate = true } } //if it's not duplicate add our array of users if !isduplicate { let userquery = pfuser.query() userquery?.wherekey("objectid", equalto: object["to"] as! string) userquery?.findobjectsinbackgroundwithblock({ (users, error ) -> void in if let users = users { user in users{ let followed = user as! pfuser self.resultdata.addobject(followed) self.users.append(followed.objectid!) counter++ } } }) } } } //reload data print(self.resultdata.count) print(counter) let array:nsarray = self.resultdata.reverseobjectenumerator().allobjects self.resultdata = nsmutablearray(array: array) self.tableview.reloaddata() } if counter == 0 { //handle no results } } loadingindicator.hidden = true loadingindicator.stopanimating() } override func viewdidload() { super.viewdidload() } override func viewdidappear(animated: bool) { self.loaddata() } override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can recreated. } func tableview(tableview: uitableview, didselectrowatindexpath indexpath: nsindexpath) { } func tableview(tableview: uitableview, numberofrowsinsection section: int) -> int { return resultdata.count } func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell { let cell:conversationcell = tableview.dequeuereusablecellwithidentifier("conversationcell", forindexpath: indexpath) as! conversationcell let user:pfuser = self.resultdata.objectatindex(indexpath.row) as! pfuser cell.profileid = user.objectid! let name = (user.objectforkey("firstname") as! string) + " " + (user.objectforkey("lastname") as! string) cell.name.text = name if user.objectforkey("profilepicture") != nil { if let profilepicture = user.objectforkey("profilepicture") as? pffile { profilepicture.getdatainbackgroundwithblock({ (data, error) -> void in if error == nil { let profileimage = uiimage(data: data!) cell.profilepicture.image = profileimage?.rounded?.circle } }) } } else{ cell.profilepicture.image = uiimage(named: "profile_picture_yellow.png")!.rounded?.circle } return cell } @ibaction func backbuttonpressed(sender: anyobject) { self.dismissviewcontrolleranimated(true, completion: {}); } }
the problem findobjectsinbackgroundwithblock updating array after reloaded user data, changed
do{ let users = try userquery?.findobjects() user in users!{ let followed = user as! pfuser self.resultdata.addobject(followed) self.users.append(followed.objectid!) counter++ } } catch let err{ print(err) }
Comments
Post a Comment