1

「OperationQueuesを使用する必要がありました」というメッセージが表示されることはわかっていますが、元の実装では問題がありました..実際には、これと同じエラーが発生していました。どちらも私を混乱させましたが、これはさらに混乱しました。

で同期される追加メソッドがoperationsあり、ワーカーも同期されます。

問題は、(少なくとも私の知る限り)選択されたオブジェクトがまだリストに含まれていないことです。次に、同じものに対して execute を 2 回呼び出し、最初のものをスキップします。

ABCが追加された場合。ACC を実行する可能性があり、B は表示されません。他のクラスは見ることができoperationsず、それに追加される唯一のものはaddOperation

プログラムからの出力の一部。

[Queue Manager] Adding new operation [ 111 ] is <SendOperation: 0x1dd5c510>
[Queue Manager] executing operation [ 112 ]  is <SendOperation: 0x1dd5c510>
[Operation]  executing [ 112 ]
[Queue Manager] Adding new operation [ 112 ] is <SendOperation: 0x1dd266e0>
[Queue Manager] executing operation [ 112 ]  is <SendOperation: 0x1dd266e0>

それは正しいアドレスを取得します。しかし、番号が間違っています。この数値は、Operation オブジェクトの単なる int です。それは建設上唯一のセンです。セッターや変更された方法はありません。

何が起きてる?

どういうわけか、それを別のアドレスに複製しているように見えますか?

#import "MyOperationQueue.h"
#import "Operation.h"
@implementation MyOperationQueue

NSMutableArray *operations;
NSCondition *condition;

-(id) init {
    self = [super init];
    if(self != nil) {
        operations = [[NSMutableArray alloc] init];
        condition = [[NSCondition alloc] init];
    }
    return self;
}

- (void) start{
    [self performSelectorInBackground:@selector(run) withObject:self];
}

-(void)run{
    while(YES){

        [condition lock];
        [condition wait];

        NSMutableArray *buffer = [[NSMutableArray alloc] init];
        @synchronized(operations){
            while([operations count] != 0 ) {
                Operation *operation = [operations objectAtIndex:0];
                [operations removeObject:operation];

                NSLog(@"[Queue Manager] executing operation %i is %@",[operation getNumber],operation);
                [operation execute];
            }
        }
        [condition unlock];
    }

}

-(void)addOperation:(id)operation{
    @synchronized(operations){ 
        NSLog(@"[Queue Manager] Adding new operation [ %i ] is %@",[operation getNumber],operation);
        [operations addObject:(operation)];
    }
    [sendCondition signal];
}

@end


@implementation Operation

int idNumber;

-(id) initWithIdNumber:(int)idNumber_{
    self = [super init];
    if( self ) {
        idNumber = idNumber_;
    }
    return self;
}


- (void) execute{
    NSLog(@"[Operation]  executing [ %i ]",idNumber);
}

-(int) getIdNumber{
    return idNumber;
}

@end
4

1 に答える 1