这真让我烦恼......所以我已经制作了一个足够基本的app来查询servlet。
它工作正常,直到我昨晚做了一些改动。
使用ASIHTTPRequest的调用正在同步工作,并且它正在返回我期望的内容,但是当我使用NSOperationQueue进行异步调用时,它会在requestFinished()中返回完全不同的响应;
任何人都可以确定它可能出错的地方吗?它可能很简单,但我已经盯着它好几个小时了,无法确定原因。
- (IBAction)searchStockOfCatNum: (id) sender
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
if (![self queue]) {
[self setQueue:[[[NSOperationQueue alloc] init] autorelease]];
}
for (int i = 0; i < self.ids.count; i++)
{
currStoreID = [self.ids objectAtIndex: i];
NSURL *url = <website removed>;
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
request.userInfo = [[NSMutableDictionary alloc] initWithObjectsAndKeys:[NSString stringWithFormat:@"%d", i], @"idx", nil];
[request setDelegate:self];
[request setDidFinishSelector:@selector(requestFinished:)];
[request setDidFailSelector:@selector(requestFailed:)];
[[self queue] addOperation:request];
}
[pool release];
}
- (void)requestFinished:(ASIHTTPRequest *)request
{
NSData *responseData = [request responseData];
NSString *responseStr = [request responseString];
}
@interface websiteViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
@property (strong, nonatomic) NSMutableArray *names, *ids, *stockLvls;
@property (strong, nonatomic) IBOutlet UITextField *catNum;
@property (strong, nonatomic) IBOutlet UIButton *searchBtn;
@property (strong, nonatomic) NSString *currStoreID;
@property (strong, nonatomic) NSOperationQueue *queue;
- (IBAction)searchStockOfCatNum: (id) sender;
正如我所说,它使用同步调用按预期工作,但在异步调用时我一直得到不正确的servlet定义。
提前致谢。