ios - Returning method object from inside block -
i wondering how following correctly: have method return nsdata
object. gets nsdata
object uidocument
. nsdata
object can large, want make sure loaded before response starts. therefore return value of method within block itself. this:
- (nsdata*)getmydata { myuidocument *doc = [[myuidocument alloc] initwithfileurl:fileurl]; [doc openwithcompletionhandler:^(bool success) { if (success) { return doc.myresponsedata; // return method not block } }]; }
this causes error because return
apparently refers block
's return
.
how can accomplish without having make thread blocking wait/while loop?
thanks.
you can't. embrace fact you're trying asynchronous , add completion block parameter getmydata
method called when inner completion handler called. (and remove return
method signature):
- (void)getmydatawithcompletion:(void(^)(nsdata *data))completion { myuidocument *doc = [[myuidocument alloc] initwithfileurl:fileurl]; [doc openwithcompletionhandler:^(bool success) { completion((success ? doc.myresponsedata : nil)); }]; }
the same problem exists in swift , can add similar completion block:
func getmydata(completion: ((data: nsdata?) -> void) { data = ... completion(data) }
Comments
Post a Comment