我正在使用NSOperationQueue,我想添加新的NSOperations到NSOperationQueue.它是一个排队,生活在一个类的单身实例.如果我可以通过传递代理将所有内容移动到静态类中,这将使事情变得更加容易.
这是我现在的代码,因为它在一个cellForRowAtIndexPath
NSString *key = [NSString stringWithFormat:@"%@%@",cell.dataItem.ItemID,cell.dataItem.ManufacturerID]; if (![self.imgOperationInQueue valueForKey:key]) { ImageOperation *imgOp = [[ImageOperation alloc] initWithItemID:cell.dataItem.ItemID withManufacturerID:cell.dataItem.ManufacturerID withReurnType:kThumbnail]; imgOp.identifier = [NSString stringWithFormat:@"%i",cell.tag]; imgOp.delegate = self; [[SharedFunctions sharedInstance] addImageOperationToQueue:imgOp]; [imgOp release]; // store these in the dictionary so we don;t queue up requests more than once [self.imgOperationInQueue setValue:cell.dataItem.ItemID forKey:key]; }
如果我可以添加委托作为参数,我可以将所有这些代码放入共享单例类中,并从我的应用程序的任何地方调用它.
我想我可以使用NSNotification – 或者我可以使用某种块?
解决方法
只需创建适当的init方法传递给委托.
- (id)initWithItemID:(NSString *)itemID withManufacturerID:(NSString *)manufacturerID withReurnType:(NSInteger)type delegate:(id<YourDelegate>)theDelegate { self = [super init]; if (self) { .... // Other assignments self.delegate = theDelegate; } return self; }