Iphone ObjC+中的回调;热可可
我对Cocoa/ObjC比较陌生。有人能帮我把代码改成使用异步网络调用吗?目前看起来像这样(虚构的例子): 请注意,对于每个操作(攻击、移动等),Networker类都具有与ServerData进行转换的逻辑。期望代码中的其他类处理此ServerData是不可接受的 我需要将A行设置为异步调用。这样做的正确方法似乎是使用[NSURLConnection connectionWithRequest:…delegate:…]实现回调以进行后处理。这是我能想到的唯一方法:Iphone ObjC+中的回调;热可可,iphone,objective-c,cocoa,events,refactoring,Iphone,Objective C,Cocoa,Events,Refactoring,我对Cocoa/ObjC比较陌生。有人能帮我把代码改成使用异步网络调用吗?目前看起来像这样(虚构的例子): 请注意,对于每个操作(攻击、移动等),Networker类都具有与ServerData进行转换的逻辑。期望代码中的其他类处理此ServerData是不可接受的 我需要将A行设置为异步调用。这样做的正确方法似乎是使用[NSURLConnection connectionWithRequest:…delegate:…]实现回调以进行后处理。这是我能想到的唯一方法: //Networker.m
//Networker.m
-(void)attack:(Charactor*)target delegate:(id)delegate {
// prepare attack information to be sent to server
ServerData *data = ...;
self._currRequestType = @"ATTACK";
self._currRequestDelegate = delegate;
[self sendToServer:data];
// extract and return the result of the attack
}
-(void)moveTo:(NSPoint*)point delegate:(id)delegate {
// prepare move information to be sent to server
ServerData *data = ...;
self._currRequestType = @"MOVE";
self._currRequestDelegate = delegate;
[self sendToServer:data];
// extract and return the result of the movement
}
-(void)sendToServer:(ServerData*)data {
// json encoding, etc
[NSURLConnection connectionWithRequest:...delegate:self] // (A)
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
//json decoding, etc
switch( self._currRequestType ) {
case @"ATTACK": {...} // extract and return the result of the attack in a callback
case @"MOVE": {...} // extract and return the result of the move in a callback
}
}
然而,这是非常丑陋的,并且不是线程安全的正确的方法是什么?
谢谢,一个选项是每个命令/请求有一个对象实例;作为一个额外的好处,您可以使用子类,从而使类型的处理是多态的,而不是基于一个大的switch语句。使用initWithDelegate:method(然后在与需要参数的命令相对应的子类中有专门的init)和基本发送/接收管道的方法创建一个基本命令对象。每个子类都可以实现一个HandlerResponse:或从基类ConnectiondFinishLoading:调用的类似函数
如果您想对该服务的客户端隐藏该对象,则攻击:、移动到:,等方法可以隐藏这些对象的实例化,因此客户端将与相同的API交互。一个选项是每个命令/请求有一个对象实例;作为一个额外的好处,您可以使用子类,从而使类型的处理是多态的,而不是基于一个大的switch语句。使用initWithDelegate:method(然后在与需要参数的命令相对应的子类中有专门的init)和基本发送/接收管道的方法创建一个基本命令对象。每个子类都可以实现一个HandlerResponse:或从基类ConnectiondFinishLoading:调用的类似函数
如果您想对该服务的客户端隐藏它,那么您的攻击:、移动到:,等等。方法可以隐藏这些对象的实例化,因此客户端将与相同的API交互。您能告诉我您的代码的虚构示例对任何人都有什么好处吗?我不得不说,把邻居的车送到机械师那里是相当重要的,因为你的机器出了问题。用一个简单的游戏类比来说明我的设计问题比解释我正在开发的应用程序有点复杂的功能更容易。你能告诉我你的代码有什么好的虚构的例子吗?有人吗?我不得不说,把邻居的车送到机械师那里是相当重要的,因为你的机器出了毛病。用一个简单的游戏类比来说明我的设计问题比解释我正在开发的应用程序有点复杂,更容易。如果你尝试一下,您可能会发现,它不像您想象的那么多额外的代码——您所需要做的就是将现有代码分散到几个类中。但是是的,有时候好的设计需要更多的代码。可理解性和可维护性应该是比原始行数更重要的考虑因素。实施起来很痛苦,但至少现在我可以跟踪当前请求并停止/重试。如果您尝试一下,您可能会发现,它不像您想象的那么多额外的代码——您所需要做的就是将现有代码分散到几个类中。但是是的,有时候好的设计需要更多的代码。可理解性和可维护性应该是比原始行数更重要的考虑因素。实现起来很痛苦,但至少现在我可以跟踪当前请求并停止/重试它。
//Networker.m
-(void)attack:(Charactor*)target delegate:(id)delegate {
// prepare attack information to be sent to server
ServerData *data = ...;
self._currRequestType = @"ATTACK";
self._currRequestDelegate = delegate;
[self sendToServer:data];
// extract and return the result of the attack
}
-(void)moveTo:(NSPoint*)point delegate:(id)delegate {
// prepare move information to be sent to server
ServerData *data = ...;
self._currRequestType = @"MOVE";
self._currRequestDelegate = delegate;
[self sendToServer:data];
// extract and return the result of the movement
}
-(void)sendToServer:(ServerData*)data {
// json encoding, etc
[NSURLConnection connectionWithRequest:...delegate:self] // (A)
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
//json decoding, etc
switch( self._currRequestType ) {
case @"ATTACK": {...} // extract and return the result of the attack in a callback
case @"MOVE": {...} // extract and return the result of the move in a callback
}
}