iphone - Callbacks in ObjC+Cocoa -


I'm relatively new to coco / obesity Do anyone help me change my code to use asynchronous network calls Can do? Currently it looks like (fictitious example):

  // networker M - (HarmatarSult *) Attack: (character * *) Set up attack information sent to target {// server server * Data = ...; ID result data = [sender sent: data]; // Repeat the attack and return the attack as an attack}} - (Movresalt *) Hiluito: (NSPoint *) dot {// server server data data; ...; ID result data = [sender sent: data]; // Remove and return the result of movement as a MoveResult} - (ServerData *) sendToServer: (ServerData *) data {// json encoding, etc. [NSURLConnectionSynchronousRequest: request ...]; // (A) // Jason decoding // Removal and result of action or request return}  

Note that for every action (attack, move, etc.), logic of the network class To convert server data and convert this server, it is unacceptable to expect other sections of my code to deal with data.

I need to make an asynchronous call to line A. It seems that the right way to do this [with NSRR connection connection. Request: ... Representative: ...] Applying callback to post-processing. This is the only way I can think of doing this:

  // Networker.m - (zero) attack: (character * *) target representative: (ID) representative {// Preparing Attack Information ServerData * Data = ...; Self._currRequestType = @ "ATTACK"; Self._currRequestDelegate = Representative; [Self-sending server: data]; // Removal of the attack and the return of the attack} - (zero) Step: (NSPoint *) Point Representative: (ID) representative {// server server; Self._currRequestType = @ "move"; Self._currRequestDelegate = Representative; [Self-sending server: data]; // Remove the results of the movement and come back} - (void) sendToServer: (ServerData *) data {// json encoding, etc. [request with NSURL connection connection: ... rep: auto] // (a)} - (Zero) ConnectionDidFinish Loading: (NSURLConnection *) Connection {// json decoding, etc switch (self._currRequestType) {case @ "ATTACK": {...} // In a callback case, to remove the outcome of the attack and Back to "" MOVE ": {...} // Remove the result of running in a callback and return back to}}  

However, it is very ugly and threads Is not protected. What is the proper way to do this?

Thanks,

per order / request for one option Should be an object example; As an additional bonus, you can then use subclasses so that type handling is polymorphic based on a large switch statement. Create a base command object with an initWithDelegate: method (after which the required sub-complexes for the commands have special shapes that require logic) and methods of sending basic / pipelines. Each subclass can apply a handle response: or your baseclass connection is said to be loading the diffinish.

If you want to hide from this service customers, then your attack:, Hilotio:, etc. By the way these objects can be hidden immediately, so customers will interact with the same API.


Comments

Popular posts from this blog

c# - ListView onScroll event -

PHP - get image from byte array -

Linux Terminal Problem with Non-Canonical Terminal I/O app -