簡體   English   中英

在不交叉所有請求的情況下,如何做很多NSURLConnection?

[英]How can I do a lot of NSURLConnection without cross over all the request?

使用NSOperation可以做到嗎? 我嘗試過,但是有時不起作用。

例如我的序列是:

  1. 將4條新聞標記為已讀
  2. 將3條新聞標記為已讀
  3. 將3條新聞標記為已讀
  4. 重新加載新聞來源
  5. 將4條新聞標記為已讀
  6. 重新加載新聞來源

有時我認為不遵守隊列...我該怎么辦? 謝謝

示例* **

NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:delegate.reader
                                                                        selector:@selector(setUnread:)
                                                                          object:item];

[delegate.queue addOperation:operation];
[operation release];

我認為其他人要求在第一個開始后開始這樣...但是我要的是第二個僅在第一個結束后才開始...

您需要在操作之間創建依賴關系,因此您可以進行如下某種條件調整:

[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

CustomNSURLConnection.m

#import "CustomNSURLConnection.h"

@implementation CustomNSURLConnection

@synthesize serviceBlock, serviceResponseData;

-(id)initWithRequest:(NSURLRequest *)request delegate:(id)delegate  usingCallback:(ServiceBlock)callBackBlock{
    self = [super initWithRequest:request delegate:delegate];
    if (self) {
        self.serviceBlock = callBackBlock;
        self.serviceResponseData = [NSMutableData data];
    }
    return self;  
}


@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];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM