nsthread

iOS how can i perform multiple NSInputStream

你说的曾经没有我的故事 提交于 2021-02-08 08:05:57
问题 My app uses NSInputStream like below: inputStream.delegate = self; [inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; [readStream open]; and delegate: - (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent It works fine, but all other requests that i do, it queued until first is finished. I can do one per time and there is no way to do multiple concurrent requests. There is a solution ? Thank you This solution not work for me : https:

多线程&NSObject&NSThread&NSOperation&GCD

心已入冬 提交于 2020-05-05 12:13:15
1、NSThread 每个NSThread对象对应一个线程,量级较轻(真正的多线程) 以下两点是苹果专门开发的“并发”技术,使得程序员可以不再去关心线程的具体使用问题 2、NSOperation/NSOperationQueue 面向对象的线程技术 3、GCD —— Grand Central Dispatch(派发) 是基于C语言的框架,可以充分利用多核,是苹果推荐使用的多线程技术 以上这三种编程方式从上到下,抽象度层次是从低到高的,抽象度越高的使用越简单,也是Apple最推荐使用的。但是就目前而言,iOS的开发者,需要了解三种多线程技术的基本使用过程。因为很多框架技术分别使用了不同多线程技术。 NSThread: 优点:NSThread 比其他两个轻量级,使用简单 缺点:需要自己管理线程的生命周期、线程同步、加锁、睡眠以及唤醒等。线程同步对数据的加锁会有一定的系统开销 NSOperation: 不需要关心线程管理,数据同步的事情,可以把精力放在自己需要执行的操作上 NSOperation是面向对象的 GCD: Grand Central Dispatch是由苹果开发的一个多核编程的解决方案。iOS4.0+才能使用,是替代NSThread, NSOperation的高效和强大的技术 GCD是基于C语言的 NSObject的多线程方法——后台线程 (void

performSelector NSThread issue

筅森魡賤 提交于 2020-02-25 04:36:08
问题 Is it possible to cancel a thread while performSelector waitsUntilDone? See below: I have a while loop with following line: [self performSelector:@selector(getMyRecords:) onThread:myThread withObject:i waitUntilDone:YES] Sometimes, I need to cancel my thread myThread while my loop executes. I get following crash issue: Terminating app due to uncaught exception 'NSDestinationInvalidException', reason: '*** -[MyController performSelector:onThread:withObject:waitUntilDone:modes:]: target thread

performSelector NSThread issue

末鹿安然 提交于 2020-02-25 04:35:35
问题 Is it possible to cancel a thread while performSelector waitsUntilDone? See below: I have a while loop with following line: [self performSelector:@selector(getMyRecords:) onThread:myThread withObject:i waitUntilDone:YES] Sometimes, I need to cancel my thread myThread while my loop executes. I get following crash issue: Terminating app due to uncaught exception 'NSDestinationInvalidException', reason: '*** -[MyController performSelector:onThread:withObject:waitUntilDone:modes:]: target thread

NSOperation - Forcing an operation to wait others dynamically

回眸只為那壹抹淺笑 提交于 2020-01-19 19:02:18
问题 I am trying to implement an operation queue and I have the following scenario: NSOperation A NSOperation B NSOperation C NSOperation D NSOperationQueue queue I start adding A to queue . During the execution of A I need to get some data from B and I can't continue with A until B returns what I need. The same situation will occur for B depending on C and for C depending on D . To manage this, at each NSOperation I have this code: NSOperation *operation; //This can be A, B, C, D or any other

RetainCount OK to use in this instance?

ⅰ亾dé卋堺 提交于 2020-01-11 10:07:28
问题 RetainCount == BAD retainCount is taboo, unreliable, unpredictable, and in general shouldn't be used. I don't use it anywhere in my code, but I have seen it in one class that I use in an interesting way. I have a class that runs a thread that runs indefinitely until the thread is cancelled. The catch is that the thread increases the retain count of the owner, in my case the class that instantiated it. So, even if I am done using that class, that instance is still going to hang around unless

core data update in background

偶尔善良 提交于 2020-01-11 07:47:34
问题 I need to basically update my core data in a background thread without blocking UI and save it. After saving should reload table View to view the changes. So for doing this I thought of using dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ // Add code here to do background processing NSManagedObjectContext *context = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType]; self.backgroundManagedObjectContext = context; if(self

Objective-C 后台线程 和 NSOperationQueue

旧街凉风 提交于 2020-01-07 04:28:59
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 后台线程 简单操作,无需创建线程时,可以使用一下函数(以UILabel 为例): [myLabel performSelector:@selector(setText:) withObject:@"hello" afterDelay:0.1f]; 代替平时的操作:[myLabel setText:@"hello"]; 1)创建线程:执行线程函数,将函数要执行的任务从主界面线程中分离出来 [NSThread detachNewThreadSelector:@selector(listenForRequests) toTarget:self withObject:nil ]; -(void)listenForRequests { @autoreleasepool { //doSomething ........ } } 注意:线程函数内创建的任何对象在函数执行完成以后不会得到协防,为防止内存泄漏,在线程函数中需使用@autoreleasepool{}进行内存管理 。 RunLoop 说到 NSThread 就不能不说起与之关系相当紧密的 NSRunLoop。Run loop 相当于 win32 里面的消息循环机制,它可以让你根据事件/消息(鼠标消息,键盘消息,计时器消息等)来调度线程是忙碌还是闲置。

How to achieve concurrent task through NSOperation? [closed]

南楼画角 提交于 2020-01-01 19:35:15
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 7 years ago . I am new to NSoperation . NSoperation is a singleshot object. How can we achieve multiple operations concurrently through NSoperation

Threaded NSTimer

这一生的挚爱 提交于 2019-12-31 04:25:06
问题 I am aware of the many questions regarding this topic, as I myself have asked one previously however, my issue now seems to be more related to the threading part. I have the following 2 methods. -(void) restartTimer { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init]; self.timer = [NSTimer scheduledTimerWithTimeInterval:1. target:self selector:@selector(dim) userInfo:nil repeats:YES]; time = 31; NSLog(@"calling restart timer"); [self performSelectorOnMainThread:@selector