我的应用程序是这样工作的: - 创建一个相册并拍照 - 在我的服务器上发送它们 - 在图片分析后获得答案/补充信息。
我对发送部分有一些问题。这是代码
@interface SyncAgent : NSObject <SyncTaskDelegate>
@property NSOperationQueue* _queue;
-(void)launchTasks;
@end
@implementation SyncAgent
@synthesize _queue;
- (id)init
{
self = [super init];
if (self) {
self._queue = [[NSOperationQueue alloc] init];
[self._queue setMaxConcurrentOperationCount:1];
}
return self;
}
-(void) launchTasks {
NSMutableArray *tasks = [DataBase getPendingTasks];
for(Task *t in tasks) {
[self._queue addOperation:[[SyncTask alloc] initWithTask:t]];
}
}
@end
和 SyncTask :
@interface SyncTask : NSOperation
@property (strong, atomic) Task *_task;
-(id)initWithTask:(Task *)task;
-(void)mainNewID;
-(void)mainUploadNextPhoto:(NSNumber*)photoSetID;
@end
@implementation SyncTask
@synthesize _task;
-(id)initWithTask:(Task *)task {
if(self = [super init]) {
self._task = task;
}
return self;
}
-(void)main {
NSLog(@"Starting task : %@", [self._task description]);
// checking if everything is ready, sending delegates a message etc
[self mainNewID];
}
-(void)mainNewID {
__block SyncTask *safeSelf = self;
[[WebAPI sharedClient] createNewPhotoSet withErrorBlock:^{
NSLog(@"PhotoSet creation : error")
} andSuccessBlock:^(NSNumber *photoSetID) {
NSLog(@"Photoset creation : id is %d", [photoSetID intValue]);
[safeSelf mainUploadNextPhoto:photoSetID];
}];
}
-(void)mainUploadNextPhoto:(NSNumber*) photoSetID {
//just admit we have it. won't explain here how it's done
NSString *photoPath;
__block SyncTask *safeSelf = self;
[[WebAPI sharedClient] uploadToPhotosetID:photoSetID withPhotoPath:photoPath andErrorBlock:^(NSString *photoPath) {
NSLog(@"Photo upload error : %@", photoPath);
} andSuccessBlock:^(NSString *photoPath) {
NSLog(@"Photo upload ok : %@", photoPath);
//then we delete the file
[safeSelf mainUploadNextPhoto:photoSetID];
}];
}
@end
每个网络操作都使用 AFNetworking 以这种方式完成:
-(void)myDummyDownload:(void (^)(NSData * data))successBlock
{
AFHTTPClient* _httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"http://www.google.com/"]];
[_httpClient registerHTTPOperationClass:[AFHTTPRequestOperation class]];
NSMutableURLRequest *request = [_httpClient requestWithMethod:@"GET" path:@"/" nil];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
AFHTTPRequestOperation *operation = [_httpClient HTTPRequestOperationWithRequest:(NSURLRequest *)request
success:^(AFHTTPRequestOperation *operation, id data) {
if(dataBlock)
dataBlock(data);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Cannot download : %@", error);
}];
[operation setShouldExecuteAsBackgroundTaskWithExpirationHandler:^{
NSLog(@"Request time out");
}];
[_httpClient enqueueHTTPRequestOperation:operation];
}
[self._queue setMaxConcurrentOperationCount:1]
我的问题是:我的连接是异步进行的,因此每个任务都是一起启动的,即使使用in也无需等待前一个任务完成SyncAgent
。
我需要同步执行每个连接吗?我认为这不是一个好主意,因为永远不应该以这种方式进行连接,还因为我可能会在其他地方使用这些方法并需要在后台执行它们,但我找不到更好的方法。任何的想法 ?
哦,如果我的代码中有任何错误/错别字,我可以向您保证,它是在我尝试在粘贴之前对其进行总结时出现的,它现在可以正常工作。
谢谢 !
PS:抱歉,我找不到更好的方法来解释问题。
编辑:我发现使用信号量更容易设置和理解:如何等待异步调度的块完成?