ios - How to sync 2 async fetches in Parse? -
so have pfobject contains 2 other pfobjects b c value. when construct local object a, need both of objects b c. first fetch doing query:
let query = pfquery(classname: "a") query.getobjectinbackgroundwithid("1", block: { (a, error) -> void in
then b , c a
var b = a!["b"] as! pfobject var c = a!["c"] as! pfobject
then need fetch b , c objects individually
b.fetchinbackgroundwithblock({ (fetchedb, error) -> void in
the problem is, fetching methods async , if put them in same thread won't guaranteed have both of them fetched in end.
one solution have fetching nested in call-backs once 1 fetch done start other.
b.fetchinbackgroundwithblock({ (fetchedb, error) -> void in c.fetchinbackgroundwithblock({ (fetchedc, error) -> void in println(fetchedb) println(fetchedc) // have values var mya = a(validb: fetchedb, validc: fetchedc) // construction can continue
but if more objects needed, become nesting in nesting. like:
b.fetchinbackgroundwithblock({ (fetchedb, error) -> void in c.fetchinbackgroundwithblock({ (fetchedc, error) -> void in d.fetchinbackgroundwithblock({ (fetchedd, error) -> void in e.fetchinbackgroundwithblock({ (fetchede, error) -> void in
however b,c,d , e not dependent on each other - should perfect fetch them on separate threads!
so there way make call-backs wait each other @ point in main thread make sure objects fetched?
thanks!
use includekey()
method on first pfquery
tell parse fetch both class b , c when performing query. relevant documentation includekey
Comments
Post a Comment