Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.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
Objective c 如何从另一个线程访问主线程的对象?_Objective C_Multithreading_Automatic Ref Counting - Fatal编程技术网

Objective c 如何从另一个线程访问主线程的对象?

Objective c 如何从另一个线程访问主线程的对象?,objective-c,multithreading,automatic-ref-counting,Objective C,Multithreading,Automatic Ref Counting,我有一些代码: //in the interface file //... NSMutableSet * someSet; //... // //in the implementation file //-(id)init { if(self = [super self]) { someSet = [[NSMutableSet alloc] init]; } return self; } -(void)someFunc { NSLo

我有一些代码:

//in the interface file
//...
NSMutableSet * someSet;    
//...
//

//in the implementation file
//-(id)init {
   if(self = [super self])
   {
       someSet = [[NSMutableSet alloc] init];
   }
   return self;
}    
-(void)someFunc
{
    NSLog(@"someSet count: %lu", [someSet count]); //always shows some number
    array = ... //getting array from another function
    for(SomeObject * obj in array)
    {
         NSSomeOperation * op = [[NSSomeOperation alloc] initWithValue:[obj someValue]];
         //start operation
         [someSet addObject:[obj someValue]];
    }  
}
//this function is called from another thread (from operation) through notification
// 
-(void)someAnotherFunc
{
    @synchronized(self)
    {
        int size = [someSet count]; //HERE size = 0, but it must be some count
        NSLog(@"someSet: %@", someSet); // it outputs: "someSet: {()}"
        NSLog(@"someSet count: %lu", [someSet count]); // it outputs: "someSet count: (null)"
    } 
}
问题是,someAnotherFunc中的size=0,但它必须是某个计数。我知道,这是因为从另一个线程调用了someAnotherFunc,在该线程中someSet=nil

我已经尝试过performSelectorOnMainThread,但没有任何帮助

更新:

我已经调试了它,调用了两次someFunc,someSet在这些调用中是有效的对象,并且有一些对象计数。我已经在someFunc中记录了count,并输出了对象的有效计数

仅当启用ARC时,在启用ARC之前发生-一切正常。

这似乎是来自线程或ARC的一些安全措施,以防止使用来自不同线程的数据。也许我不对,但我不明白为什么会这样

在执行这段代码的过程中,someSet不会直接分配给nil。而且它没有被解除分配


有人知道如何从另一个线程访问对象someSet吗?如何获得someSet中对象的有效计数?

该代码没有与线程相关的错误(除了尝试以这种方式执行线程会遇到大量不相关的问题)

在某个地方,
someSet
在另一个线程的代码运行之前被设置为nil。你说这只发生在启用ARC的情况下?那么这很可能是因为您没有对封装该代码的任何对象的强引用;i、 e.由于编译器已确定不再使用该对象,因此正在取消分配该对象

这很可能是因为处理通知的代码没有设置为保留观察者(保留对观察者的引用)。在全局变量中直接或间接粘贴对观察者的引用

请注意,该代码中的
@synchronize()
可能是无意义的,或者,如果确实有多个线程可能触发通知,则表明存在架构问题


还要注意,
NSLog(@“someSet count:%@,[someSet count]);//它输出:“someSet count:(null)”
如果
someSet
设置正确,将崩溃。

给我们更多信息,你调试过吗?在调用count时,从另一个线程打印出someSet的值?您是否尝试过
@synchronized
-ing someSet?是的,@synchronized(self)已包含在内。我会更新问题,谢谢。@fbernardo我已经更新了问题,你能再看一遍吗?对不起,我对ARC不太了解,对我来说一切都很好…不要担心这段代码不会崩溃,因为它永远不会编译
someSet=[NSMutableSet alloc]init]
@NJones是的,你说得对,我忘记了“[”。每个人都可以输入错误。谢谢,someSet没有被释放。传递引用帮助解决了这个问题。但我不明白为什么会发生这种情况,因为someSet没有被释放,并且在这两种情况下都有有效的对象计数。