mso*_*ler 6 network-programming ios mknetworkkit mknetworkengine
我是新手MKNetworkKit,但我已经能够将它添加到我的项目中,除非处理可达性更改,否则它完全正常工作.
情况如下:
POST也通过创建一个MKNetworkOperationfrom我的MKNetworkEngine子类来请求(使用)一些数据.在请求数据之前,操作被设置为freezable(根据Mugunth Kumar的文档).checkAndRestoreFrozenOperationsin MKNetworkEngine并且它检测到有一个挂起的操作(创建的没有可达性),它尝试入队.onCompletion块永远不会被调用.有什么我不明白冻结操作+可达性MKNetworkKit吗?冻结是否仅适用于请求开始后可达性发生变化的操作?或者我应该实现自己的可达性更改块?
这是我的MKNetworkEngine子类中的代码,它创建操作并启动请求.请注意,不相关的代码已被禁止.
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObject:@"value" forKey:@"param"];
MKNetworkOperation *op = [self operationWithPath:MYPATH
params:params
httpMethod:@"POST"];
[op setFreezable:YES];
[op onCompletion:^(MKNetworkOperation *completedOperation) {
// ...
// Here is where I process response and send the result to my completion block
// It's called when WiFi is available, but not called otherwise.
// ...
} onError:^(NSError *error) {
// It's called when WiFi is available, but not called otherwise.
DLog(@"Some error");
}];
[self enqueueOperation:op];
return op;
Run Code Online (Sandbox Code Playgroud)
这是两个独立的问题:简历与完成.
resume: Freeze/Unfreeze机制仅在启用了缓存时才有效
您必须在AppDelegate -didFinishLaunchingWithOptions中调用-useCache:
self.networkEngine = [[MKNetworkEngine alloc] init ...];
[self.networkEngine useCache]; // <- Add this line
Run Code Online (Sandbox Code Playgroud)complete:在网络状态更改时不会调用完成回调(即在Unfreeze之后)
然而,如果您采取行动(1.)并在行中的MKNetworkOperation.m -checkAndRestoreFrozenOperations中放置一个断点:
[self enqueueOperation:pendingOperation]
Run Code Online (Sandbox Code Playgroud)
您将发现在恢复网络连接时调用它,并且pendingOperation 是您的待处理POST.但是,由于新的MKNetworkOperation已经实例化(到那时完成块可能不再存在),onCompletion因此永远不会调用您的块.一种可能的解决方法是使用通知而不是回调.
完全修复:比(2)更强大的方法可以在启动时^{}使用NSNotifications 替换块回调.尽早注册您的听众,就像在AppDelegate中一样.以下是使MKNetworkKit通知精通所需的最小更改:
3A.在MKNetworkOperation.h中插入通知常量
#define MKNetworkOperationCompletionNotification @"MKNetworkOperationCompletionNotification"
#define MKNetworkOperationErrorNotification @"MKNetworkOperationErrorNotification"
Run Code Online (Sandbox Code Playgroud)
3B.在MKNetworkOperation.m -operationSucceeded中广播成功通知(请注意,我使用postNotificationOnMainThread,以便可以从主线程收听所述通知并更改UI;请参阅主线程上的NSOperation和NSNotificationCenter):
-(void) operationSucceeded {
NSDictionary * aUserInfo = [NSDictionary dictionaryWithObjectsAndKeys:
self, NSStringFromClass([MKNetworkOperation class]),
nil];
NSNotification * notification = [NSNotification notificationWithName:MKNetworkOperationCompletionNotification
object:nil
userInfo:aUserInfo];
[[NSNotificationCenter defaultCenter] postNotificationOnMainThread:notification];
...
Run Code Online (Sandbox Code Playgroud)
3C.在MKNetworkOperation.m -operationFailedWithError中广播失败通知
-(void) operationFailedWithError:(NSError*) error {
self.error = error;
NSDictionary * aUserInfo = [NSDictionary dictionaryWithObjectsAndKeys:
self, NSStringFromClass([MKNetworkOperation class]),
error, NSStringFromClass([NSError class]),
nil];
NSNotification * notification = [NSNotification notificationWithName:MKNetworkOperationErrorNotification
object:nil
userInfo:aUserInfo];
[[NSNotificationCenter defaultCenter] postNotificationOnMainThread:notification];
...
Run Code Online (Sandbox Code Playgroud)
3D.将一个相当持久的对象(如AppDelegate)注册为侦听器(不要忘记取消注册):
// Listen to POST changes
NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
[defaultCenter addObserver:self
selector:@selector(mkNetworkOperationCompletionNotification:)
name:MKNetworkOperationCompletionNotification
object:nil];
[defaultCenter addObserver:self
selector:@selector(mkNetworkOperationErrorNotification:)
name:MKNetworkOperationErrorNotification
object:nil];
Run Code Online (Sandbox Code Playgroud)
3E.监听器的示例代码:
- (void)mkNetworkOperationCompletionNotification:(NSNotification*)notification {
MKNetworkOperation *operation = [[notification userInfo]
objectForKey:NSStringFromClass([MKNetworkOperation class])];
NSLog(@"operationSucceeded: %@", [operation responseString]);
}
- (void)mkNetworkOperationErrorNotification:(NSNotification*)notification {
NSError * error = [[notification userInfo] objectForKey:NSStringFromClass([NSError class])];
NSLog(@"operationFailedWithError: %@", [error localizedDescription]);
}
Run Code Online (Sandbox Code Playgroud)这样做,你已经完成了.X.
(编辑在上一个答案中删除了对MKNetworkOperation.h不必要的建议更改,并添加了第3段以显示如何克服^ {}限制.)
| 归档时间: |
|
| 查看次数: |
2172 次 |
| 最近记录: |