nsrunloop

Timer in another thread in Objective - C

喜夏-厌秋 提交于 2020-08-21 06:41:13
问题 I have to define method which should be invoked periodically with some time interval. I need to invoke it in another thread (NOT main thread), because this method is used to get information from external API and sync data in core data. How do I define this method to not block main thread? 回答1: Unless you have a specific need for timers, you can use Grand Central Dispatch. The following snippet will execute a block after 2 seconds, on the default priority concurrent queue (i.e a background

Timer in another thread in Objective - C

给你一囗甜甜゛ 提交于 2020-08-21 06:41:09
问题 I have to define method which should be invoked periodically with some time interval. I need to invoke it in another thread (NOT main thread), because this method is used to get information from external API and sync data in core data. How do I define this method to not block main thread? 回答1: Unless you have a specific need for timers, you can use Grand Central Dispatch. The following snippet will execute a block after 2 seconds, on the default priority concurrent queue (i.e a background

NSRunLoop&NSTimer混合使用

牧云@^-^@ 提交于 2020-03-01 17:15:43
// // ViewController.m // NSRunLoop // // Created by JackMeng on 13-11-14. // Copyright (c) 2013 年 JackMeng. All rights reserved. // #import "ViewController.h" @interface ViewController () @end @implementation ViewController - ( void )viewDidLoad { [ super viewDidLoad ]; // 使用 NSTimer 创建定时器 NSTimeInterval timeInterval = 1 ; // 间隔时间 [ NSTimer scheduledTimerWithTimeInterval :timeInterval target : self selector : @selector (timerMethod) userInfo : nil repeats : YES ]; // 使用 RunLoop 创建 NSTimer 对象 NSRunLoop *theRunLoop = [ NSRunLoop currentRunLoop ]; // 获得当前的 RunLoop NSDate *fireDate = [ NSDate

Difference in usage of function sleep() and [[NSRunLoop currentRunLoop] runUntilDate]

眉间皱痕 提交于 2020-01-31 18:17:50
问题 Please consider the following pieces of code: In the first one i call a function which creates an animation. i do that with a certain time interval: start:; [self animationMethod]; [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:3]]; //sleep(3); goto start; In the second one i create an animation - (void)animationMethod { CAKeyframeAnimation *myAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"]; CGMutablePathRef curvedPath = CGPathCreateMutable();

Difference in usage of function sleep() and [[NSRunLoop currentRunLoop] runUntilDate]

☆樱花仙子☆ 提交于 2020-01-31 18:13:12
问题 Please consider the following pieces of code: In the first one i call a function which creates an animation. i do that with a certain time interval: start:; [self animationMethod]; [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:3]]; //sleep(3); goto start; In the second one i create an animation - (void)animationMethod { CAKeyframeAnimation *myAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"]; CGMutablePathRef curvedPath = CGPathCreateMutable();

Using [[NSRunLoop currentRunLoop] runUntilDate:[NSDate date]] to let the scheduled selectors fire

允我心安 提交于 2020-01-25 01:58:07
问题 In my application at some point I have a bunch of messages scheduled using performSelector . Under some conditions, while handling an UI action, I need to wait for all the currently scheduled selectors to fire. I could place my code in another method and schedule it using performSelector:target:argument:order:modes: with order value high enough to be sure it will fire last, but there are reasons why I think that would make an ugly solution. So I send [[NSRunLoop currentRunLoop] runUntilDate:

upload files in background via ftp on iphone

☆樱花仙子☆ 提交于 2020-01-14 04:43:11
问题 i'm trying to establish an FTP connection within an app. i want to upload several files to a FTP server, all files in one directory. So at first i want to create the remote directory. - (void) createRemoteDir { NSURL *destinationDirURL = [NSURL URLWithString: uploadDir]; CFWriteStreamRef writeStreamRef = CFWriteStreamCreateWithFTPURL(NULL, (__bridge CFURLRef) destinationDirURL); assert(writeStreamRef != NULL); ftpStream = (__bridge_transfer NSOutputStream *) writeStreamRef; BOOL success =

Is calling -[NSRunLoop runUntilDate:] a good idea?

泄露秘密 提交于 2020-01-10 01:41:06
问题 Is it generally a good idea to call -[NSRunLoop runUntilDate:] ? It seems to work without any issues, but it makes me nervous to tell the run loop to run from within the run loop. More info: I have a project right now that is fetching data from a REST service. One critical piece of information that needs to be obtained is the range of dates with valid data. It's a very small bit of data that only needs to be gotten once, so I decided that the best way to handle it is to have the property

NSURLConnectionDelegate methods not called when using NSThread

五迷三道 提交于 2020-01-05 15:23:51
问题 I'm trying to run a download in a background thread as to not block the main UI thread on iOS, what I did was create a new thread using [NSThread detachNewThreadSelector:@selector(startDownload) toTarget:downloadObject withObject:nil]; Then the following code runs on a background thread: NSURL* urlForCalendar = [NSURL URLWithString:@"http://www.apple.com/"]; urlRequest = [NSURLRequest requestWithURL:urlForCalendar]; urlConnection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate

Cocoa: No keyboard events will fire while dragging (NSEventTrackingRunLoopMode)

纵然是瞬间 提交于 2020-01-05 07:47:07
问题 I am successfully able to react to keyboard events through my window controller's keyDown: method. The problem arises while performing a mouse drag: Keyboard events seem to be delayed and will only fire on mouse up. To be clear, what I mean is: • place a log statement in you window controller's keyDown: method • launch your app, perform some drag operation (on a NSSlider for ex.) • while maintaining the drag, press any key: nothing logs to the console. • release drag : logs appear, yay… The