3

我开发的应用程序是一个与 OS X 服务器通信的 iOS 客户端。这个应用程序的当前版本在主线程上完成了所有的网络逻辑,这对于我想做的事情来说很好。

但是,在下一个版本中,我希望网络逻辑更加灵活。为此,我想专门为其创建一个单独的线程,但我不太确定哪种解决方案适合我的需求。

起初,GCD 看起来是一个不错的候选者,但它似乎只适合在单独的线程上执行大块工作。我想做的是将所有网络逻辑放在一个单独的线程上。iOS 客户端和 OS X 服务器之间的连接是持久的,所有数据流和处理都应该发生在那个单独的线程上。

问题归结为,哪种方法最适合这种情况?

编辑:为了消除任何混淆,我使用的连接使用套接字和 NSStream 实例。我不是在处理连接到远程 Web 服务器的问题。换句话说,AFNetworking 和 ASIHttpRequest 不是我的选择。

4

2 回答 2

6
  1. 您可以使用 runloop(我们称之为 NetworkThread)创建一个线程,运行以下代码:

    while (!self.isCancelled) {
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
        [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
        [pool release];
    }
    
  2. 然后您可以使用- (void)performSelector:(SEL)aSelector onThread:(NSThread *)thr withObject:(id)arg waitUntilDone:(BOOL)wait在 NetworkThread 上执行您的网络请求选择器。

  3. 所有网络回调将在 NetworkThread 上调用,然后在 NetworkThread 上处理您的响应数据,将最终数据推送到主线程,更新 UI。

于 2012-04-19T10:25:46.743 回答
0

AFNetworking 很棒。使用块和 GCD。我编写了一个 NetworkClient 类,它使得调用它变得相当简单。随意使用它或定制它,但我不保证任何东西:)

如果 API 密钥不符合您的需要,可以将其删除。我将它添加为一种为我的所有网络请求增加安全性的方法。我的 Web 服务在返回任何内容之前检查传入的 APIKey 的有效性。

此外,您可以相当灵活地完成您想要的事情。您可以执行同步或异步请求、不警告用户失败的请求等。

基本示例用法:

NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
                                @"GetAllParkingLots", @"Command",
                                nil];

        [NetworkClient processURLRequestWithURL:nil andParams:params block:^(id obj) {
            // I check to see what kind of object is passed back, in this case I was expecting an Array
            if ([obj isKindOfClass:[NSArray class]]) {
                // Do something with the Array
                [self processNetworkRequestWithArray:(NSArray *)obj];
            }
        }];

网络客户端.h:

//
//  NetworkClient.h
//
//  Created by LJ Wilson on 2/18/12.
//  Copyright (c) 2012 LJ Wilson All rights reserved.
//

#import <Foundation/Foundation.h>

extern NSString * const APIKey;

@interface NetworkClient : NSObject

+(void)processURLRequestWithURL:(NSString *)url 
                      andParams:(NSDictionary *)params 
                          block:(void (^)(id obj))block;

+(void)processURLRequestWithURL:(NSString *)url 
                      andParams:(NSDictionary *)params 
                    syncRequest:(BOOL)syncRequest
                          block:(void (^)(id obj))block;

+(void)processURLRequestWithURL:(NSString *)url 
                      andParams:(NSDictionary *)params 
                    syncRequest:(BOOL)syncRequest
             alertUserOnFailure:(BOOL)alertUserOnFailure
                          block:(void (^)(id obj))block;

+(void)handleNetworkErrorWithError:(NSError *)error;

+(void)handleNoAccessWithReason:(NSString *)reason;

@end

网络客户端.m:

//
//  NetworkClient.m
//
//  Created by LJ Wilson on 2/18/12.
//  Copyright (c) 2012 LJ Wilson All rights reserved.
//

#import "NetworkClient.h"
#import "AFHTTPClient.h"
#import "AFHTTPRequestOperation.h"
#import "SBJson.h"

#warning Set APIKey or set to nil
NSString * const APIKey = @"YourAPIKEYGoesHere";

@implementation NetworkClient

+(void)processURLRequestWithURL:(NSString *)url 
                      andParams:(NSDictionary *)params 
                          block:(void (^)(id obj))block {

    [self processURLRequestWithURL:url andParams:params syncRequest:NO alertUserOnFailure:NO block:^(id obj) {
        block(obj);
    }];
}

+(void)processURLRequestWithURL:(NSString *)url 
                      andParams:(NSDictionary *)params 
                    syncRequest:(BOOL)syncRequest
                          block:(void (^)(id obj))block {
    [self processURLRequestWithURL:url andParams:params syncRequest:syncRequest alertUserOnFailure:NO block:^(id obj) {
        block(obj);
    }];
}


+(void)processURLRequestWithURL:(NSString *)url 
                      andParams:(NSDictionary *)params 
                    syncRequest:(BOOL)syncRequest
             alertUserOnFailure:(BOOL)alertUserOnFailure
                          block:(void (^)(id obj))block {

#warning Fix default url
    // Default url goes here, pass in a nil to use it
    if (url == nil) {
        url = @"YourDefaultURLGoesHere";
    }

    NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithDictionary:params];
    [dict setValue:APIKey forKey:@"APIKey"];

    NSDictionary *newParams = [[NSDictionary alloc] initWithDictionary:dict];

    NSURL *requestURL;
    AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:requestURL];

    NSMutableURLRequest *theRequest = [httpClient requestWithMethod:@"POST" path:url parameters:newParams];

    __block NSString *responseString = @"";

    AFHTTPRequestOperation *_operation = [[AFHTTPRequestOperation alloc] initWithRequest:theRequest];
    __weak AFHTTPRequestOperation *operation = _operation;

    [operation  setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        responseString = [operation responseString];

        id retObj = [responseString JSONValue];

        // Check for invalid response (No Access)
        if ([retObj isKindOfClass:[NSDictionary class]]) {
            if ([[(NSDictionary *)retObj valueForKey:@"Message"] isEqualToString:@"No Access"]) {
                block(nil);
                [self handleNoAccessWithReason:[(NSDictionary *)retObj valueForKey:@"Reason"]];
            }
        } else if ([retObj isKindOfClass:[NSArray class]]) {
            if ([(NSArray *)retObj count] > 0) {
                NSDictionary *dict = [(NSArray *)retObj objectAtIndex:0];
                if ([[dict valueForKey:@"Message"] isEqualToString:@"No Access"]) {
                    block(nil);
                    [self handleNoAccessWithReason:[(NSDictionary *)retObj valueForKey:@"Reason"]];
                }
            }
        }
        block(retObj);
    } 
                                      failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                                          NSLog(@"Failed with error = %@", [NSString stringWithFormat:@"[Error]:%@",error]);
                                          block(nil);
                                          if (alertUserOnFailure) {
                                              // Let the user know something went wrong
                                              [self handleNetworkErrorWithError:operation.error];
                                          }

                                      }];

    [operation start];

    if (syncRequest) {
        // Process the request syncronously
        [operation waitUntilFinished];
    } 


}


+(void)handleNetworkErrorWithError:(NSError *)error {
    NSString *errorString = [NSString stringWithFormat:@"[Error]:%@",error];

    // Standard UIAlert Syntax
    UIAlertView *myAlert = [[UIAlertView alloc] 
                            initWithTitle:@"Connection Error" 
                            message:errorString 
                            delegate:nil 
                            cancelButtonTitle:@"OK" 
                            otherButtonTitles:nil, nil];

    [myAlert show];

}

+(void)handleNoAccessWithReason:(NSString *)reason {
    // Standard UIAlert Syntax
    UIAlertView *myAlert = [[UIAlertView alloc] 
                            initWithTitle:@"No Access" 
                            message:reason 
                            delegate:nil 
                            cancelButtonTitle:@"OK" 
                            otherButtonTitles:nil, nil];

    [myAlert show];

}


@end
于 2012-04-19T09:51:06.603 回答