0

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

我认为其他人要求在第一个 1 之后这样开始......但我想要的是第二个只有在第一个完成后才开始......

4

3 回答 3

0

您需要在操作之间创建依赖关系,因此您可以进行某种调节,如下所示:

[secondOperation addDependency:firstOperation];
[operationQueue addOperation:firstOperation];
[operationQueue addOperation:secondOperation];
于 2013-07-19T13:28:06.773 回答
0

在我看来,您正在寻找一个串行操作队列。

只需将maxConcurrentOperationCount队列上的设置为1,它一次只会运行一个操作。

这应该在将操作添加到队列之前完成。请注意,它只需要执行一次,而不是像我在下面显示的每个操作。

delegate.queue.maxConcurrentOperationCount = 1;
[delegate.queue addOperation:someOperation];
于 2013-07-19T13:35:23.500 回答
0

创建一个 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 请求

于 2013-07-19T13:33:26.940 回答