标签: nsthread

NSThread VS pthreads

NSThread和pthread有什么区别?NSThread是否使用pthread作为其内部实现,当pthreads特别是在iPhone上时,我们真的需要额外的NSThread开销.

cocoa-touch pthreads objective-c nsthread

6
推荐指数
1
解决办法
3218
查看次数

NSThreads,NSOperations和performSelector之间的区别

我想问一些关于iPhone开发的简单而重要的问题.如果我们必须在后台执行任务并且后台任务完成后我们将更新UI,为此我们可以使用NSThreads,NSOperations或(performSelector)performSelectorInBackgroundThread.所有这些以及它们将如何影响我的应用程序性能之间的区别是什么.

还有一件事,这两个陈述之间的区别是什么: -

[self getData];

[self performSelector:@selector(getData)];

Please explain as i dont know the difference between all these things. 
Run Code Online (Sandbox Code Playgroud)

iphone nsthread

6
推荐指数
2
解决办法
6587
查看次数

如何取消或停止NSThread?

我正在做一个应用程序,在读取XML文件时使用NSThread加载viewControllers的内容.

我做到如下:

-(void)viewDidAppear:(BOOL)animated
{
    // Some code...


    [NSThread detachNewThreadSelector:@selector(loadXML) toTarget:self withObject:nil];
    [super viewDidAppear:YES];
}

-(void)loadXML{

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    // Read XML, create objects...

    [pool release];
}
Run Code Online (Sandbox Code Playgroud)

我的问题是,如果用户在加载NSThread时更改为另一个viewController,我不知道如何停止NSThread,这样做应用程序崩溃了.

我试图取消或退出NSThread如下,但没有成功:

-(void)viewsDidDisappear:(BOOL)animated{
    [NSThread cancel];
    // or [NSThread exit];
    [super viewDidDisappear:YES];
}
Run Code Online (Sandbox Code Playgroud)

有人可以帮忙吗?谢谢.

iphone sdk nsthread ios

6
推荐指数
1
解决办法
9117
查看次数

Objective C&iOS:运行计时器?的NSTimer /主题/的NSDate的/ etc

我正在开发我的第一个iOS应用程序,并且遇到了第一个问题我无法找到一个好的答案.

问题:我有一个自定义UIGestureRecognizer并且所有连线都正确连接,我可以@selector在识别后为每次触摸运行代码.这对大多数事情来说都很好,但对其他人来说这是一个太多的输入.

我的目标:制作一个以指定间隔触发的计时器来运行逻辑,并且能够在取消触摸时取消此计时.

我在这里问的原因:解决方案有很多可能性,但没有一个能够成为最好的解决方案.到目前为止似乎

  • performSelector (以及对此的一些变化)
  • NSThread
  • NSTimer
  • NSDate
  • 操作队列
  • 我想我也发现了其他一些......

从所有的研究来看,某种形式的线程似乎是可行的路线,但我不知道哪种方式最适合这种情况.

实现的示例:NSPoint每0.10秒采用一次,并且采用前一点和当前点之间的距离.[取每个点之间的距离产生非常混乱的结果].

相关代码:

- (void)viewDidLoad {
 CUIVerticalSwipeHold *vSwipe =
 [[CUIVerticalSwipeHold alloc]
 initWithTarget:self 
 action:@selector(touchHoldMove:)];
 [self.view addGestureRecognizer:vSwipe];
 [vSwipe requireGestureRecognizerToFail:doubleTap];
}

...

- (IBAction)touchHoldMove:(UIGestureRecognizer *)sender {
     if (sender.state == UIGestureRecognizerStateEnded) {

     }

     if (sender.state == UIGestureRecognizerStateBegan) {

     }

     //other stuff to do goes here

}
Run Code Online (Sandbox Code Playgroud)

objective-c nsdate nstimer nsthread ios

6
推荐指数
1
解决办法
6611
查看次数

如何从通过NSthread调用的函数中获取返回值?

我写了一个返回一个对象的函数.但它是一个从web获取数据的函数.所以NSthread用于调用函数.

 NSThread* pageThread = [[NSThread alloc]initWithTarget:self selector:@selector(threadFunction) object:nil];
Run Code Online (Sandbox Code Playgroud)

如何从通过NSthread调用的函数中获取返回值?

iphone objective-c nsthread ipad ios

6
推荐指数
1
解决办法
681
查看次数

Objective C中最好的多线程方法?

我正在开发一个iPad应用程序,我目前正在努力寻找多线程的最佳方法.让我用一个简单的例子来说明这一点:
我有一个包含2个子视图的视图,一个目录选择器和一个图库,其中包含所选目录中所有图像的缩略图.由于"下载"并生成这些缩略图可能需要很长时间才需要多线程,因此视图的交互和更新不会被阻止.

这就是我已经尝试过的:
[self performSelectorInBackground:@selector(displayThumbnails :) withObject:currentFolder];
这工作正常,因为用户交互没有被阻止,但是当用户在第一个文件夹仍在加载时点击另一个文件夹时,它很可能失败.两个线程试图访问相同的视图和变量,这导致彼此搞乱正确执行.当用户点击另一个文件夹时,displayThumbnails当前加载的文件夹应该被中止.我没有找到任何办法这样做..

NSThreads
我试过这个但是遇到了与第一种方法几乎相同的问题,我没有找到一种(简单的)方法来取消正在进行的方法.(是的,我知道[aThread cancel]但没有办法'恢复'线程).也许我应该NSThread继承并实现我自己的isRunning等方法?但是,有没有更好的方法或第三(甚至第四和第五)选项我忽略?

我认为这是一个相当简单的例子,我认为没有子类化可能有更好的解决方案NSThread.那么,你会做什么?请你的意见!

cocoa multithreading objective-c nsthread ios

6
推荐指数
2
解决办法
3998
查看次数

NSRunLoop做什么?

我读到很多帖子NSRunLoop,像这个,这个,这个.但无法弄清楚究竟NSRunLoop做了什么

我通常看到的是一个工人线程

wthread = [[NSThread alloc] initWithTarget:self selector:@selector(threadProc) object:nil];  
[wthread start];
Run Code Online (Sandbox Code Playgroud)

里面有一个NSRunLoop

- (void)threadProc 
{
    NSAutoreleasePool* pool1 = [[NSAutoreleasePool alloc] init];
    BOOL isStopped = NO;
    NSRunLoop *runloop = [NSRunLoop currentRunLoop];
    [runloop addPort:[NSMachPort port] forMode:NSDefaultRunLoopMode];

    while (!isStopped)
    {
        {
            NSAutoreleasePool* pool2 = [[NSAutoreleasePool alloc] init];
            [runloop runMode:NSDefaultRunLoopMode
                                      beforeDate:[NSDate distantFuture]];

            [pool2 release];
        }
    }

    [pool1 release];
}
Run Code Online (Sandbox Code Playgroud)

主线程将一些工作传递给了这个wthread

[self performSelector:@selector(someWork:) onThread:wthread withObject:nil waitUntilDone:NO];
Run Code Online (Sandbox Code Playgroud)

在将工作从主线程传递到工作线程方面,我看到很多人这样做.为什么需要NSRunLoop?它有什么作用 ?

我读了NSRunLoop用于管理事件,为什么会出现什么叫除外runMode里面threadProc

port nsthread nsrunloop ios

6
推荐指数
1
解决办法
7680
查看次数

何时释放/保留传递给辅助线程的对象?

我使用以下代码将对象传递给辅助线程:

 (void)login:(id)sender
{
  platformMsgs_LoginRequest *loginRequest = [[[platformMsgs_LoginRequest alloc] init] autorelease];
//more code...
 [NSThread detachNewThreadSelector:@selector(sendLoginRequest:) toTarget:self withObject:loginRequest];
//more code...
}

- (void)sendLoginRequest:(platformMsgs_LoginRequest *)loginRequest
 {
 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
 [loginRequest retain];
 NetSuiteBinding *binding = [NetSuiteServiceSvc NetSuiteBinding];
 NetSuiteBindingResponse *response = [binding loginUsingParameters:loginRequest      applicationInfo:nil partnerInfo:nil];
 [self performSelectorOnMainThread:@selector(loginOperationCompleted:)   withObject:response waitUntilDone:NO];
 [loginRequest release];
 [pool drain];
 }
Run Code Online (Sandbox Code Playgroud)

我的问题是,自动释放正确的方法来处理这个对象的释放吗?一旦它传递给辅助线程,我保留它并在我不再需要它时释放它.

但是,自动释放可能会在辅助线程有机会保留它之前释放对象吗?我是否必须为此创建一个ivar?以便我可以在performSelectorOnMainThread中释放该对象?登录后我不再需要该对象,因此ivar似乎不是正确的方法.处理这个问题的最佳方法是什么?谢谢.

-Oscar

iphone variables arguments retain nsthread

5
推荐指数
1
解决办法
545
查看次数

如何在不立即执行选择器的情况下创建不是主线程的NSThread

我想创建一个不是主线程的工作线程,所以我可以调用...

[self performSelector:@selector(doStuff) OnThread:self.myWorkerThread withObject:nil];
Run Code Online (Sandbox Code Playgroud)

...在我的代码中的一堆位置.如何创建线程..任何线程并将其分配给变量,以便我可以在我的代码中反复使用它.NSThread detachNewThreadWithSElector只会创建一个用于运行该选择器的目的.我需要创建一个线程并以某种方式使用它.

我正在使用iOS,需要在writerThread上执行所有CoreData写操作.我每次都需要使用相同的线程(不是主线程).

nsthread ios

5
推荐指数
1
解决办法
1万
查看次数

performSelector:onThread:在Swift中?

在当前的iOS应用程序中,我使用此执行选择器方法:

[self performSelector:@selector(doSomething)
             onThread:myThread
           withObject:nil
        waitUntilDone:NO
                modes:[NSArray arrayWithObject:NSRunLoopCommonModes]];
Run Code Online (Sandbox Code Playgroud)

我不知道如何在swift中的特定线程上运行选择器.有什么建议?

nsthread ios performselector swift

5
推荐指数
1
解决办法
1699
查看次数