nstimer

第四十三篇、利用NSProxy解决NSTimer内存泄漏问题

◇◆丶佛笑我妖孽 提交于 2020-02-11 01:01:37
问题描述:   用NSTimer来实现每隔一定时间执行制定的任务,例如最常见的广告轮播图。如果我们在 timerWithTimeInterval:1 target:self 中指定target为当前控制器,控制器则会被timer强引用,而控制器对timer也是强引用的。一般,我们终止定时器往往在界面销毁时,即dealloc方法中写 [_timer invalidate];。基于上面的分析,由于循环引用的存在,控制器永远也不会走dealloc方法,定时器会一直执行方法,造成内存泄露。 解决方案:   利用消息转发来断开NSTimer对象与视图之间的引用关系。初始化NSTimer时把触发事件的target替换成一个单独的对象,然后这个对象中NSTimer的SEL方法触发时让这个方法在当前的视图self中实现。 背景知识:   NSProxy:NSProxy 是一个抽象类,它接收到任何自己没有定义的方法他都会产生一个异常,所以一个实际的子类必须提供一个初始化方法或者创建方法,并且重载forwardInvocation:方法和methodSignatureForSelector:方法来处理自己没有实现的消息。 从类名来看是代理类,专门负责代理对象转发消息的。相比NSObject类来说NSProxy更轻量级,通过NSProxy可以帮助Objective-C间接的实现多重继承的功能。 代码:

iOS how to call a method on the minute, every minute

大城市里の小女人 提交于 2020-02-05 05:38:37
问题 I mean 12:01:00, 12:02:00 ... In iOS7, I want to call a method when minute of time is changed. In other words, if now is 01:55:47, then I want to call a method at 01:56:00 -> 01:57:00 -> 01:58:00 ... All I found about call a method is just about TimeInterval, but not I want. I've tried this code: NSTimeInterval roundedInterval = round([[NSDate date] timeIntervalSinceReferenceDate] / 60.0) * 60.0; NSDate *date = [NSDate dateWithTimeIntervalSinceReferenceDate:roundedInterval]; NSTimer *timer =

58同城iOS面试题

安稳与你 提交于 2020-02-05 05:11:59
58金融 一面 1、KVO的具体实现?比如A要知道B的属性的变化? 2、单列怎么释放? 3、GCD里面放NSTimer,可以正常定时吗? 4、算法:无序数组找最大和最小值?最优方案 5、设计题:接口数据返回list里面只有type字段,怎么做到cell里面没有if else或者switch语句 6、NSUserDefaults的使用 7、cocoaPods里面pod install和update的区别 8、cocoaPods里面Podfile.lock是什么 9、frame和masonry哪个性能好?为什么 10、category项目中用到了哪些? 11、NSTimer一定要加入runloop吗 12、什么是离屏渲染? 13、了解autoreleasePool吗?什么时机释放? 14、什么创建出来的对象就是会有autoreleased?在arc中什么时机加入? 15、gcd可以取消吗?gcd如何cancel?dispatch_block_cancel 16、异步请求多个数据,数据回来后怎么保证完整性 17、创建一个单列,它是在栈还堆区? 二面 1、目前职业管理和开发占的比例?3:7 2、怎么管理项目,有没有代码规范 3、https有哪些安全性? ssl + ca 4、block的截获变量 5、SDWebImage的原理 6、单列 然后把它设为nil 生效吗 7、NSTimer精确吗

OC-内存管理-NSTimer

风流意气都作罢 提交于 2020-01-31 05:34:07
NSTimer 就是我们的定时器 可以满足我们很多业务需求 ,但是它会带来内存泄漏的隐患. 场景:在一个VC中设定一个全局定时器定时打印log,需要在pop该VC的时候 清除定时器 减少内存泄露 解决如下: 001 生命周期 处理 - ( void )didMoveToParentViewController:(UIViewController *)parent{ NSLog(@"didMoveToParent==%@",parent); //parent 就是当前的视图控制器的UINavigationController if (parent == nil ) { [ self .timer invalidate]; self .timer = nil ; } } 002 设置消息转发 @property ( nonatomic , strong ) id target; class_addMethod([_target class], @selector(fire), (IMP)fireImp,"V@:"); self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:_target selector:@selector(fire) userInfo:nil repeats:YES]; [[NSRunLoop

Why are my audio sounds not playing on time?

怎甘沉沦 提交于 2020-01-23 17:04:45
问题 One of my apps has a simple metronome-style feature that plays a click sound a specified number of times per minute (bpm). I'm doing this by starting an NSTimer, with an interval calculated from the specified bpm, that calls a method that plays the sound. If I put an NSLog line into the play method, I can see that NSTimer is firing accurately to about 1 millisecond. However, if I record the sound output into an audio editor and then measure the interval between clicks, I can see that they are

Why are my audio sounds not playing on time?

十年热恋 提交于 2020-01-23 17:04:11
问题 One of my apps has a simple metronome-style feature that plays a click sound a specified number of times per minute (bpm). I'm doing this by starting an NSTimer, with an interval calculated from the specified bpm, that calls a method that plays the sound. If I put an NSLog line into the play method, I can see that NSTimer is firing accurately to about 1 millisecond. However, if I record the sound output into an audio editor and then measure the interval between clicks, I can see that they are

AVAudioPlayer cannot play music with a NSTimer in background

自作多情 提交于 2020-01-23 03:59:25
问题 I want to play music with an AVAudioPlayer using an NSTimer, but [player prepareToPlay] returns NO & doesn't play in the background. Can somebody give me some idea? Here's my code: - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(playMusic:) userInfo:nil repeats:NO]; [self.window makeKeyAndVisible]; return YES; } - (void)playMusic:(NSTimer *)timer{ [

NSTimer callbacks while iPhone application is inactive

ぐ巨炮叔叔 提交于 2020-01-22 15:05:18
问题 This question seems to be the essence of several others on this forum. I believe that it is possible for the active iPhone application to continue running, and specifically, to continue receiving timer call-backs, after it has entered the inactive state (either through the idle timer kicking in the screen lock, or through the user pressing the hardware lock button). The documentation specifically says that while an application is inactive, it is executing, but not dispatching incoming events

Change the time interval of a Timer

故事扮演 提交于 2020-01-21 23:44:46
问题 here is my question: Is it possible to increase the scheduledTimerWithTimeInterval:2 for example of "3" after 10 seconds in ViewDidLoad for example. E.g., from this: [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(createNewImage) userInfo:nil repeats:YES]; to this: [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(createNewImage) userInfo:nil repeats:YES]; thank you sorry for my english I french :/ 回答1: Reschedule the timer recursively like this

Update Text Label From Another View

戏子无情 提交于 2020-01-17 16:38:45
问题 I have an NSTimer that runs every 10 seconds and is kicked off from LandingController.m. It continues to run as you go to other views in the application. I want to be able to (when a certain condition is met within that timer) update a label field from another view GuardMenu.m The label I want to update is called CurrentZone.text and I want to update it from value "N" to value "Y." Here's my timer on LandingController.m self.messageTimer = [NSTimer scheduledTimerWithTimeInterval:10.0 target