Nir Tzin Nir Tzin - 1 year ago 109
Swift Question

swift 2 UITableView Refresh after Almofire

I know almofire works as a thread in the background so I put in my main class

let nc = NSNotificationCenter.defaultCenter()
nc.addObserver(self, selector: "taskdataReadyFunc", name: "taskdataReady", object: nil)

and in my dataClass after the almofire finish:

init() {
Alamofire.request(.GET, urlString, parameters: parameters).responseJSON { response in
if response.result.isSuccess {
let json = JSON(response.result.value!)
let data = json.arrayValue
self.tasks = data


// print(self.tasks)
print(String(self.tasks.count)+"before nc")
let nc = NSNotificationCenter.defaultCenter()
nc.postNotificationName("taskdataReady", object: nil)


that leads to thet function:

func taskdataReadyFunc (){

print("reload mision table" + String(taskDataClass.sharedInstance.tasks.count))


This worked fine when I start the app for the first time.
my problem is that I made a new task that adds a new task to the database and then tries to run the init of the dataClass again. at that stage, i can see the new task coming from the DB but it not make a table reload data.(if I close and open the app I see it)
how can I refresh my table view or maybe to close and open the controller from the start ?

Answer Source

the sharedInstance is create only one time when you first call it. its called singleTon. that means that if the init() function fill the variable in the class the sharedInstance will not be update.

so, in the init class, even in the first run update the sharedInstance and not the variable. example:

//self.tasks = data

taskDataClass.sharedInstance.tasks = data
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download