ios - NSXMLParser parse seems to behave asynchronously in my block -
i must overlooking silly. using afnetworking perform http request returns xml content. when request returns success block executes. using nsxmlparser parse xml , return result.
i know nsxmlparser's parse method synchronous , should block until xml parsing complete. works i'd expect in unit tests. in context of success block appears call nsxmlparser:parse , continue execution. passing (initialized) parser object nsxmlparser delegate. parsing called within object initialize member data. need parsing complete before block completes execution.
so parsing object looks this:
@interface content : nsobject <nsxmlparserdelegate> @property (readonly) nsxmlparser *xmlparser; @property (readonly) nsstring *documenttitle; @property (readonly) nsmutablearray *sectiontitles; @property (readonly) nsstring *documentformat; @property (readonly) nsstring *localization; @property (readonly) nsstring *contentversion; - (id)initwithparser:(nsxmlparser *)xmlparser; - (bool)parse; @end this object's "parse" method simple calls nsxmlparser:parse
-(bool)parse{ nslog(@"%@", @"calling parse");//this in output return [xmlparser parse]; // return parse success or error };
in service call calling constructor on object , providing nsxmlparser. call object's "parse" method. block seems call parse , continue execution (like asynchronous call) , return object uninitialized.
-(void)makehttprequest:(nsurl *)url onsuccess:(contentsuccess)success //this block client code passes in blocks onfailure:(contentfailure)failure{ nsurlrequest *request = [nsurlrequest requestwithurl:url]; afhttprequestoperation *operation = [[afhttprequestoperation alloc]initwithrequest:request]; [operation setcompletionblockwithsuccess:^(afhttprequestoperation *operation, id responseobject) { nsxmlparser *xmlparser = [[nsxmlparser alloc]initwithdata:responseobject]; content *content = [[content alloc]initwithparser:xmlparser]; [content parse]; // gets called it's not blocking success(kbcontent); // gets called right away ivar == nil } failure:^(afhttprequestoperation *operation, nserror *error) { failure(error); } ]; [operation start]; } this must have block implementation.. said [content parse] seems work synchronously in unit tests. not run in context of block. can offer advice?
thanks!
update
it seems content:parse method returning 'no' indicate parse error. nsxmlparser.pasererror 'nil'. if simple pass nsxmlparser object success block , parse has no problems. in block none of parser delegate methods ever called. strange.
if nsxmlparser instance returning parse without calling delegate methods, encountered error. check return value of parse (it return no if fails) parsererror method (which returns nserror if one's been set). it's not impossible parser set error still succeeded, make sure base decisions on return value, not presence of error (this established cocoa convention).
Comments
Post a Comment