I want to achieve the following :
- In classB, to reload my database after adding 1 object.
reloadDatabase()
is called within the completionBlock. - In classB,
reloadDatabase()
will callgetObjects()
inclassA
to get the most updated list of database objects and pass toobjectList
in classB
Question: How do i ensure that whenever i call getObjectList()
in classB, i will always get the most updated list? From my understanding, my objectList
might not be update in reloadDatabase() block
. I could be calling getObjectList()
when reloadDatabase()
haven't reach the completion block yet (objectList is still the old objectList).
I am pretty new to closures and blocks. Any guidance is greatly appreciated!
class classA: NSObject {
func addThisObject(object: RLMObject, completionBlock: () -> ())){
...
completionBlock()
}
func getObjects (completionBlock: ((list: [RLMObject]) -> ())){
var recordList = [RLMObject]()
...
completionBlock(list: recordList)
}
}
class classB: NSObject {
var objectList = [RLMObject]()
func addObject (object: RLMObject) {
classA().addThisObject(object, completionBlock: {() -> () in
self.reloadDatabase()
})
}
func reloadDatabase() {
classA().getObjects{(list) -> () in
self.objectList = list
}
}
func getObjectList() -> [RLMObject] {
return objectList
}
}