标签: nsthread

+ [NSThread detachNewThreadSelector:toTarget:withObject:]和 - [NSObject performSelectorInBackground:withObject:]之间有什么区别?

他们似乎执行了一个相当类似的任务:启动一个快速轻松地执行该选择器的新线程.但是有什么不同吗?也许关于内存管理?

cocoa multithreading nsthread

9
推荐指数
1
解决办法
2606
查看次数

是否可以确定与打开的NSStream相关联的NSRunLoop/NSThread?

我正在使用(并且需要使用)我没有来源的第三方框架.第三方框架处理创建经过身份验证的客户端/服务器连接并回送一对打开的NSStream.

根据Apple的文档,流创建过程是:alloc/init,set delegate,schedule in run loop和open.Apple的文档更进一步说:"你永远不应该尝试从与拥有流的运行循环的线程不同的线程访问预定的流." https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Streams/Articles/ReadingInputStreams.html#//apple_ref/doc/uid/20002273-1001844

流处理过程是:关闭,取消安排,释放.

如果自己创建了一个流,则可以清楚地预定流的位置.如果第三方框架创建了流,则可能无法知道流的调度位置.

查看我找到的文档,我没有看到以编程方式确定与开放NSStream相关联的NSRunLoop和NSThread的方法.有没有办法在运行时确定此信息?

macos nsstream nsthread ios

9
推荐指数
1
解决办法
913
查看次数

iOS上的NSThread号码?

当您打印NSThread的描述时,您会得到如下内容:

<NSThread: 0x1e511b70>{name = (null), num = 1}
Run Code Online (Sandbox Code Playgroud)

这个"数字"是否以某种方式可用?

这仅用于调试,因此无需清除Apple的批准过程.

debugging nsthread ios

9
推荐指数
2
解决办法
4479
查看次数

如何在iphone中停止NSThread子线程

我在主线程中使用NSThread创建了一个子线程

NSThread *newThread = [[NSThread alloc] initWithTarget:self selector:@selector(MyThread:) object:timer];

5秒后,我在主线程中使用[newThread cancel]来停止子线程,但它没有用,

方法MyThread:在newThread中仍然有效

那么,什么是正确的答案来停止newThread,THX

实际上[newThread isCancelled]是YES,但是选择器MyThread仍在进行中

iphone objective-c nsthread

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

setKeepAliveTimeout和BackgroundTasks

我对这个话题非常头疼.我正在开发一个需要定期轮询网络服务器的应用程序,以便检查新数据.根据返回的信息,我希望将本地通知推送给用户.

我知道这种方法与Apple描述的方法略有不同,其中远程服务器根据APNS进行工作,推送远程通知.但是,我有很多理由不能考虑这种方法.一个是用户身份验证机制.出于安全原因,远程服务器无法考虑用户凭据.我所能做的就是将登录和获取核心移动到客户端(iPhone).

我注意到Apple提供了一个应用程序唤醒并保持打开Socket连接(即VoIP应用程序)的机会.

所以,我开始以这种方式进行调查.在plist中添加了所需的信息,我能够在我的appDelegate中使用类似的东西"唤醒"我的应用程序:

[[UIApplication sharedApplication] setKeepAliveTimeout:1200 handler:^{ 
    NSLog(@"startingKeepAliveTimeout");
    [self contentViewLog:@"startingKeepAliveTimeout"];
    MyPushOperation *op = [[MyPushOperation alloc] initWithNotificationFlag:0 andDataSource:nil];
    [queue addOperation:op];
    [op release];
}];
Run Code Online (Sandbox Code Playgroud)

NSOperation然后使用以下块代码启动后台任务:

#pragma mark SyncRequests
-(void) main {
    NSLog(@"startSyncRequest");
    [self contentViewLog:@"startSyncRequest"];
    bgTask = [app beginBackgroundTaskWithExpirationHandler:^{ 
        NSLog(@"exipiration handler triggered");
        [app endBackgroundTask:bgTask];
        bgTask = UIBackgroundTaskInvalid;
        [self cancel];
    }];


        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            NSMutableURLRequest *anURLRequest;
            NSURLResponse *outResponse;
            NSError *exitError;
            NSString *username;
            NSString *password;

            NSLog(@"FirstLogin");
            anURLRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:webserverLogin, username, password]]];
            [anURLRequest setHTTPMethod:@"GET"];
            [anURLRequest setTimeoutInterval:120.00];
            [anURLRequest setCachePolicy:NSURLRequestReloadIgnoringCacheData];

            exitError = nil;
            NSData …
Run Code Online (Sandbox Code Playgroud)

iphone objective-c nsurlconnection nsthread objective-c-blocks

8
推荐指数
3
解决办法
2万
查看次数

dispatch_semaphore_t重用 - 我在这里缺少什么?

我有一些代码,我使用dispatch_semaphore_t来指示操作完成.当信号量是成员变量时,它似乎行为不正确.我将展示有效的示例代码和一个似乎不起作用的示例:

@implementation someClass  
{  
  dispatch_semaphore_t memberSem;  
  dispatch_semaphore_t* semPtr;  
  NSThread* worker;
  BOOL taskDone;  
}  

- (id)init  
{  
  // Set up the worker thread and launch it - not shown here.  
  memberSem= dispatch_semaphore_create(0); 
  semPtr= NULL;  
  taskDone= FALSE;  
}  

- (void)dealloc  
{  
  // Clean up the worker thread as needed - not shown here.  
  if((NULL != semPtr) && (NULL != *semPtr))  
    disptatch_release(*semPtr);  

  dispatch_release(memberSem);  
}  

- (void)doSomethingArduous  
{  
  while([self notDone])  // Does something like check a limit.  
    [self doIt];  // Does something like process …
Run Code Online (Sandbox Code Playgroud)

semaphore objective-c nsthread grand-central-dispatch

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

在swift中创建线程?

我试图在swift中生成一个线程.所以我有这条线:

...

let thread = NSThread(target: self, selector: doSomething(), object: nil)
Run Code Online (Sandbox Code Playgroud)

...

doSomething是类的范围内的函数.

该行给出了这个错误:"找不到接受提供的参数的init()的重载"

我在这里错过了什么?我可以在swift中创建一个新线程吗?

nsthread ios swift

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

NSThread现在会自动创建autoreleasepool吗?

我有这样的测试代码

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(test) object:nil];
    [thread start];
}

-(void)test
{
    MyClass *my = [[[MyClass alloc] init] autorelease];
    NSLog(@"%@",[my description]);
}
Run Code Online (Sandbox Code Playgroud)

我没有为自己的线程创建任何自动释放池,但是当线程退出时,对象"我的"只是dealloc.why?

即使我改变我的测试代码如下

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(test) object:nil];
    [thread start];
} 

-(void)test
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    MyClass *my = [[[MyClass alloc] init] autorelease];
    NSLog(@"%@",[my description]);
}
Run Code Online (Sandbox Code Playgroud)

我创建了自己的autoreleasepool但在线程退出时不会将其耗尽.无论如何,对象"我的"仍然可以dealloc.为什么?

我使用Xcode5而不使用ARC

nsthread nsautoreleasepool ios

8
推荐指数
2
解决办法
2624
查看次数

主视图崩溃时的子视图将图像从AVFoundation分配给UIImageView时

我已经创建了一个自定义AVFoundation摄像头并作为子视图加载到另一个视图中使用@IBDesignable,在拍摄图像后我想将其分配给UIImageView,但是当按下按钮时,自定义摄像头视图(UIView)会崩溃并向上移动屏幕,而不是将其分配给UIImageView,这发生在自定义摄像头视图加载开始时,然后当我再次加载自定义摄像头视图时,它会将图像分配给UIImageView.

捕获图像方法:

@IBAction func captureImage(sender: AnyObject) {
        if let stillOutput = self.captureImageOutput {
            // we do this on another thread so that we don't hang the UI
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
                //find the video connection
                var videoConnection : AVCaptureConnection?
                for connecton in stillOutput.connections {
                    //find a matching input port
                    for port in connecton.inputPorts!{
                        if port.mediaType == AVMediaTypeVideo {
                            videoConnection = connecton as? AVCaptureConnection
                            break //for port
                        }
                    }

                    if …
Run Code Online (Sandbox Code Playgroud)

avfoundation nsthread ios swift

8
推荐指数
1
解决办法
320
查看次数

自动参考计数(ARC)中的NSThreads

我试图在4.3.5中使用NSThreads和ARC.使用iOS 5,一切都很完美,但如果我在较旧的iOS上尝试它,如4.3它的泄漏.通常我会为NSThreads使用Autoreleasepool,但由于ARC中没有手动Autoreleasepool我不知道如何解决这个问题.

我收到了大量的消息,比如"__NSAutoreleaseNoPool():类NSComparisonPredicate的对象0x4567b40自动释放,没有池到位 - 在我启动一个线程后在我的控制台中泄漏".

NSThread detachNewThreadSelector:@selector(showAlert) toTarget:self withObject:nil];
Run Code Online (Sandbox Code Playgroud)

如何在5.0之前正确地与ARC和iOS进行交互.

干杯网

iphone objective-c nsthread nsautoreleasepool automatic-ref-counting

7
推荐指数
1
解决办法
3529
查看次数