Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Multithreading 从NSOperation对象访问另一个类的属性_Multithreading_Cocoa_Concurrency - Fatal编程技术网

Multithreading 从NSOperation对象访问另一个类的属性

Multithreading 从NSOperation对象访问另一个类的属性,multithreading,cocoa,concurrency,Multithreading,Cocoa,Concurrency,亲爱的社区。 我有一个属性为的NSOperation子类: @property(readwrite, getter=isCancelled) BOOL cancelled; 从我创建子类NSObject的对象的位置(从init): 在此自定义对象中,我尝试声明本地iVar: @interface MySQLIXC : NSObject { BOOL _currentQueueStatus; 在init中: - (id)initWithQuene:(NSUInteger)que

亲爱的社区。 我有一个属性为的NSOperation子类:

@property(readwrite, getter=isCancelled) BOOL cancelled;
从我创建子类NSObject的对象的位置(从init):

在此自定义对象中,我尝试声明本地iVar:

@interface MySQLIXC :  NSObject {
       BOOL _currentQueueStatus;
在init中:

- (id)initWithQuene:(NSUInteger)quene andCarrier:(NSString *)carrierName withState:(BOOL)currentQueueStatus;
    _currentQueueStatus = currentQueueStatus;
但currentQueueStatus始终为空。 有人可以建议问题的位置吗?

1)您应该避免为添加内容重新声明已实现的子类接口(例如,
-[NSOperation isCancelled]
存在)

2) 从两个保留计数开始是非常不寻常的:

database = [[MySQLIXC alloc] initWithQuene:iQuene andCarrier:startForCarrier withState:cancelled];
[otherThingThatHoldsAReference setDatabase:database];
而不是:

database=[[mysqlix alloc]initWithQuene:iQuene和carrier:startForCarrier with state:cancelled]retain]

3)
\u currentQueueStatus
不是
null
它是一个
BOOL
,它是一个
签名字符
。它应该是
0
NO
)或
1
YES

4) 什么是
currentQueueStatus
?更多的代码将帮助您获得更具体的答案

编辑:更新以澄清评论

/* things would look different if you subclass MONOperation */
@interface MONOperation : NSOperation
{
@private
    MySQLIXC * sqlIxc;
    BOOL didCancelDatabaseRequest;
}

/*
do not use isCancelled for your name - NSOperation declares this method, and
its implementation is well defined. this would override the implementation
and likely cause runtime errors.

specifically, NSOperation/NSOperationQueue uses cancel and isCancelled in its
interface and state. if you must cancel, then call cancel from cancelDatabaseRequest.
you may override cancel, but call [super cancel] in your implementation.
*/
@property (readonly) BOOL didCancelDatabaseRequest;
@property (retain) MySQLIXC * sqlIxc;

@end

@implementation MONOperation

/* ... */

- (BOOL)didCancelDatabaseRequest
{
    return didCancelDatabaseRequest;
}

- (void)cancelDatabaseRequest /* in this example, there is no sense making cancelDatabaseRequest publicly visible */
{
/* for example */
    assert(self.sqlIxc);
    self.sqlIxc = nil;
    didCancelDatabaseRequest = YES;
    [self cancel]; /* do this rather than overriding NSOperation's cancellation interfaces in your subclass */
}

- (void)main
{
    NSAutoreleasePool * pool = [NSAutoreleasePool new];
    assert(self.sqlIxc);
    self.sqlIxc.doStuff;
    if (self.sqlIxc.didTimeout || self.sqlIxc.couldNotAccessFile) {
        [self cancelDatabaseRequest];
    }
    else {
    /* use the result */
    }
    [pool release];
}

@end

具体来说,您要在何处声明
\u currentQueueStatus
?请显示您添加到.Tnx中的代码,我想您还没有显示您在其中声明它的代码。请出示代码。Thx为你回答:1)这是一个新的声明,我希望它是正确的@property(readwrite)BOOL取消;2) 我的MySQLIXC*数据库刚刚在子类NSOperation的@interface中声明。我必须为MySQLIXC*数据库2声明其他属性(保留还是分配?),并在u示例中创建[self-setDatabase2:database]?3) 我知道我不能为null,但我在NSLog(@“MYSQL:queue status:%@”,“u currentQueueStatus”)中看到了它;4) 我是关于它的更新问题。欢迎1)它取代了NSOperation的实现-您必须选择另一个名称,例如
isDatabaseRequestCancelled
。这是一个等待发生的崩溃(NSOperation/NSOperationQueue实现将变得非常混乱)2)保留它3)您的NSLog调用将生成编译器警告。如果没有,请打开编译器警告-通过%@格式说明符将签名字符作为objc对象传递是一个等待发生的错误访问。1)NSOperation子类中的此声明我的意思是。我还得选择另一个名字?如果为true,则无法设置此属性(已启动、已取消、正在执行、已暂停、已完成)。如果你指的是子类,我试着将当前状态的属性发送到那里。在类中,我在init中对此状态有另一个声明:_currentQueueStatus=currentQueueStatus;正如你在我编辑的例子中看到的,我在回复中添加了一个例子——希望这能澄清问题亲爱的贾斯汀。Tnx是一个很有用的例子,但在这个例子中,你可以从主操作队列子类中取消。但是我需要从我的子类中取消读取状态,并在那里做一些事情(取消事务e.t.c)。你能根据这个重新写入吗?
/* things would look different if you subclass MONOperation */
@interface MONOperation : NSOperation
{
@private
    MySQLIXC * sqlIxc;
    BOOL didCancelDatabaseRequest;
}

/*
do not use isCancelled for your name - NSOperation declares this method, and
its implementation is well defined. this would override the implementation
and likely cause runtime errors.

specifically, NSOperation/NSOperationQueue uses cancel and isCancelled in its
interface and state. if you must cancel, then call cancel from cancelDatabaseRequest.
you may override cancel, but call [super cancel] in your implementation.
*/
@property (readonly) BOOL didCancelDatabaseRequest;
@property (retain) MySQLIXC * sqlIxc;

@end

@implementation MONOperation

/* ... */

- (BOOL)didCancelDatabaseRequest
{
    return didCancelDatabaseRequest;
}

- (void)cancelDatabaseRequest /* in this example, there is no sense making cancelDatabaseRequest publicly visible */
{
/* for example */
    assert(self.sqlIxc);
    self.sqlIxc = nil;
    didCancelDatabaseRequest = YES;
    [self cancel]; /* do this rather than overriding NSOperation's cancellation interfaces in your subclass */
}

- (void)main
{
    NSAutoreleasePool * pool = [NSAutoreleasePool new];
    assert(self.sqlIxc);
    self.sqlIxc.doStuff;
    if (self.sqlIxc.didTimeout || self.sqlIxc.couldNotAccessFile) {
        [self cancelDatabaseRequest];
    }
    else {
    /* use the result */
    }
    [pool release];
}

@end