Iphone 我如何在不跨越所有请求的情况下进行大量连接?
是否可以通过NSO操作来实现这一点?我试过了,但有时不起作用 例如,“我的序列”是:Iphone 我如何在不跨越所有请求的情况下进行大量连接?,iphone,ios,objective-c,nsurlconnection,Iphone,Ios,Objective C,Nsurlconnection,是否可以通过NSO操作来实现这一点?我试过了,但有时不起作用 例如,“我的序列”是: 将4条新闻标记为已读 将3条新闻标记为已读 将3条新闻标记为已读 重新加载新闻源 将4条新闻标记为已读 重新加载新闻源 有时候我觉得排队是不受尊重的。。。我该怎么办? 谢谢 示例*** NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:delegate.reader
NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:delegate.reader
selector:@selector(setUnread:)
object:item];
[delegate.queue addOperation:operation];
[operation release];
我想其他人要求像这样开始后的第一个1。。。但是我想要的是,第二个只在第一个完成后才开始…您需要在操作之间创建依赖项,这样您就可以进行类似这样的调节:
[secondOperation addDependency:firstOperation];
[operationQueue addOperation:firstOperation];
[operationQueue addOperation:secondOperation];
创建一个CustomNSURLConnection类,如下所示 CustomNSURLConnection.h
#import <Foundation/Foundation.h>
typedef void (^ServiceBlock)(NSString *result);
@interface CustomNSURLConnection : NSURLConnection
@property (nonatomic, copy) ServiceBlock serviceBlock;
@property (nonatomic, retain) NSMutableData *serviceResponseData;
- (id)initWithRequest:(NSURLRequest *)request delegate:(id)delegate usingCallback:(ServiceBlock)callBackBlock;
@end
WebServiceHitter.h
#import "CoffeeshopCustomNSURLConnection.h"
@interface WebServiceHitter : NSObject
@end
WebServiceHitter.m
@interface CoffeeShopWebServiceHitter()
@property (nonatomic, retain) NSMutableData *responseData;
#pragma mark - NSURLConnection Delegate Methods
- (void)connection:(CustomNSURLConnection *)connectionDb didReceiveResponse:(NSURLResponse *)response {
CustomNSURLConnection *specializedNSURLDataConnection = (CustomNSURLConnection *)connectionDb;
[specializedNSURLDataConnection.serviceResponseData setLength:0];
}
- (void)connection:(CustomNSURLConnection *)connectionDb didReceiveData:(NSData *)data {
CustomNSURLConnection *specializedNSURLDataConnection = (CoffeeshopCustomNSURLConnection *)connectionDb;
[specializedNSURLDataConnection.serviceResponseData appendData:data];
}
- (void)connection:(CustomNSURLConnection *)connectionDb didFailWithError:(NSError *)error {
NSLog(@"Connection failed: %@", [error description]);
}
- (void)connectionDidFinishLoading:(CustomNSURLConnection *)finishedWithConnection {
CustomNSURLConnection *specializedNSURLConnection = (CustomNSURLConnection*)finishedWithConnection;
NSString *responseString = [[NSString alloc] initWithData:specializedNSURLConnection.serviceResponseData encoding:NSASCIIStringEncoding];
specializedNSURLConnection.serviceResponseData = nil;
specializedNSURLConnection.serviceBlock(responseString);
}
@end
并根据您的使用情况在webservicehitter文件中创建您的webservice请求我觉得您好像在寻找串行操作队列 只需将队列上的
maxConcurrentOperationCount
设置为1
,它一次只运行一个操作
这应该在将操作添加到队列之前完成。请注意,它只需执行一次,而不是像我在下面展示的那样针对每个操作
delegate.queue.maxConcurrentOperationCount = 1;
[delegate.queue addOperation:someOperation];
是的,有可能吗?你试过了吗?我添加了一个我的请求的例子,如果我不知道哪个是当前的上一个操作?当前的上一个?它是当前的还是以前的?我的队列在appdelegate中。。。很多类可以在队列中添加操作。。。。所以我不知道这是之前添加为dependency的NSO操作,只有您知道顺序。。。你不知道哪个是当前正在执行的操作。这似乎有点过分,或者你能想出一个有效的理由来解释为什么OP应该这样做吗?(我做过一次,但它奏效了,这不是一个有效的论点。)这将如何解决OP的问题?为什么这是一个好的解决方案?他说他希望数据分离,连接应该通过一个类,所以只有我更喜欢这种方法我会尝试这种解决方案。。。我想这就是我一直在寻找的。。。我会让你知道的。有人想对否决票发表意见吗?什么使它成为一个糟糕的答案?
delegate.queue.maxConcurrentOperationCount = 1;
[delegate.queue addOperation:someOperation];