├── AppledocSettings.plist
├── NSTimer+JCSBlocks.h
├── NSTimer+JCSBlocks.m
└── README.textile
/AppledocSettings.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | --project-name
6 | NSTimer+JCSBlocks
7 | --create-html
8 |
9 | --create-docset
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/NSTimer+JCSBlocks.h:
--------------------------------------------------------------------------------
1 | //
2 | // NSTimer+JCSBlocks.h
3 | // atime
4 | //
5 | // Created by Abizer Nasir on 26/08/11
6 | // Copyright 2011 Jungle Candy Software. All rights reserved.
7 | // Licences under the MIT Licence. See the README for details.
8 | //
9 | // Additions to NSTimer that allow it to use blocks as callbacks
10 | // Requires compilation with ARC
11 |
12 | #import
13 |
14 | /** An NSTimer Extension that runs a block when the timer is fired.
15 |
16 | Blocks are frequently more convenient to use instead of callbacks. They appear in the same context as the code that calls
17 | them and, and it's easier to pass state information to them at the same time.
18 |
19 | This extension creates two methods, one which creates an optionally repeating timer which fires a block, and the other creates
20 | a repeating timer but passes a reference to a stop parameter to the block so the timer can be stopped from within the block.
21 |
22 | */
23 | typedef void (^JCSTimerCallback)();
24 | typedef void (^JCSInterruptableTimerCallback)(BOOL *stop);
25 |
26 | @interface NSTimer (JCSBlocks)
27 |
28 | /** Create an optionally repeating timer with a block to run when the timer is fired.
29 |
30 | @return An NSTimer that will fire events as configured.
31 | @param seconds NSInterval. The number of seconds between the the firing of the timer.
32 | @param repeats BOOL. Whether the timer should be repeating or not.
33 | @param block A block which has a void return and takes no paramaters. This is run whenever the timer fires.
34 |
35 | The type is JCSTimerCallback which is defined as:
36 |
37 | typedef void (^JCSTimerCallback)();
38 |
39 | */
40 | + (NSTimer *)jcs_scheduledTimerWithTimeInterval:(NSTimeInterval)seconds repeats:(BOOL)repeats blockHandler:(JCSTimerCallback)block;
41 |
42 |
43 | /** Create a repeating timer which can be stopped from within the passed block
44 |
45 | @return An NSTimer that will fire events as configured.
46 | @param seconds NSInterval. The number of seconds between the the firing of the timer.
47 | @param block A block which has a void return but has a `BOOL *stop` parameter.
48 |
49 | Setting this parameter to `NO` from within the block will prevent any more events to be fired by the timer.
50 | Additionally, the timer will be invalidated and set to nil when it is stopped this way.
51 |
52 | The type is JCSInterruptableTimerCallback which is defined as
53 |
54 | typedef void (^JCSInterruptableTimerCallback)(BOOL *stop);
55 |
56 | */
57 | + (NSTimer *)jcs_scheduledInterruptableTimerWithTimeInterval:(NSTimeInterval)seconds blockHandler:(JCSInterruptableTimerCallback)block;
58 |
59 | @end
60 |
--------------------------------------------------------------------------------
/NSTimer+JCSBlocks.m:
--------------------------------------------------------------------------------
1 | //
2 | // NSTimer+JCSBlocks.m
3 | // atime
4 | //
5 | // Created by Abizer Nasir on 26/08/11
6 | // Copyright 2011 Jungle Candy Software. All rights reserved.
7 | // Licences under the MIT Licence. See the README for details.
8 | //
9 |
10 | #import "NSTimer+JCSBlocks.h"
11 |
12 | @interface JCSTimerBlockHandler : NSObject
13 | @property (nonatomic, strong) id block;
14 | - (void)jcs_handleBlockWithTimer:(NSTimer *)timer;
15 | - (void)jcs_handleBlockWithInterruptableTimer:(NSTimer *)timer;
16 |
17 | @end
18 |
19 | @implementation NSTimer (JCSBlocks)
20 |
21 | + (NSTimer *)jcs_scheduledTimerWithTimeInterval:(NSTimeInterval)seconds repeats:(BOOL)repeats blockHandler:(JCSTimerCallback)block {
22 |
23 | JCSTimerBlockHandler *blockHandler = [[JCSTimerBlockHandler alloc] init];
24 | blockHandler.block = [block copy];
25 |
26 | return [self scheduledTimerWithTimeInterval:seconds target:blockHandler selector:@selector(jcs_handleBlockWithTimer:) userInfo:nil repeats:repeats];
27 | }
28 |
29 | + (NSTimer *)jcs_scheduledInterruptableTimerWithTimeInterval:(NSTimeInterval)seconds blockHandler:(JCSInterruptableTimerCallback)block{
30 |
31 | JCSTimerBlockHandler *blockHandler = [[JCSTimerBlockHandler alloc] init];
32 | blockHandler.block = [block copy];
33 |
34 | return [self scheduledTimerWithTimeInterval:seconds target:blockHandler selector:@selector(jcs_handleBlockWithInterruptableTimer:) userInfo:nil repeats:YES];
35 | }
36 |
37 | @end
38 |
39 | @implementation JCSTimerBlockHandler
40 |
41 | @synthesize block;
42 |
43 | - (void)jcs_handleBlockWithTimer:(NSTimer *)timer {
44 |
45 | JCSTimerCallback callbackBlock = self.block;
46 |
47 | callbackBlock();
48 | }
49 |
50 | - (void)jcs_handleBlockWithInterruptableTimer:(NSTimer *)timer {
51 |
52 | JCSInterruptableTimerCallback callbackBlock = self.block;
53 |
54 | BOOL stop;
55 | callbackBlock(&stop);
56 |
57 | if (stop) {
58 | [timer invalidate];
59 | timer = nil;
60 | }
61 | }
62 |
63 | @end
--------------------------------------------------------------------------------
/README.textile:
--------------------------------------------------------------------------------
1 | h1. NSTimer+JCSBlocks
2 |
3 | An extension of NSTimer that lets you use a block to be run when the timer fires instead of passing a callback method.
4 |
5 | There are two methods; one which lets you fire an optionally repeating timer with a block, and another which lets you fire a timer repeatedly, but which can be stopped from within the passed block.
6 |
7 | h2. Branch structure for submodules
8 |
9 | There are two branches to this repository, *master* and *demo*. If you just want to use the class extension, the *master* branch is the one you should be using.
10 |
11 | h3. The master Branch
12 |
13 | The *master* branch just contains the class files and this README file. This is the branch to use if you are installing the class files as a Git submodule. As submodules are added in a headless state it is often preferable to explicitly state the branch you are cloning. So this is the command I recommend you use:
14 |
15 | bc. git submodule add -b master https://github.com/Abizern/NSTimer-JCSBlocks
16 |
17 | h3. The demo Branch
18 |
19 | the *demo* branch contains a very simple example of the use of this class extension. No development of the main files should be done on that branch.
20 |
21 | Changes made to the master branch will be merged across to demo, so it should always remain current with respect to master.
22 |
23 | h2. Documentation
24 |
25 | The generation for this extension is generated from the header comments from "appledoc":http://gentlebytes.com/appledoc/, and is available "online here":http://abizern.github.com/NSTimer-JCSBlocks/docs/index.html.
26 |
27 | h2. Updates
28 |
29 | To keep up to date with the latest changes `cd` into the directory that contains this submodule and pull the newest changes as usual
30 |
31 | bc. git pull origin
32 |
33 | h2. Artefacts
34 |
35 | Sometimes, there may be artefacts left over when switching from master to production. These are files that are ignored by Git and are easily cleaned up by running
36 |
37 | bc. git clean -dxf
38 |
39 | h2. Licensed under the MIT Licence
40 |
41 | Use it, fix it, or be inspired to replace callbacks with blocks in your own code!
42 |
43 | Copyright (c) 2011 Abizer Nasir
44 |
45 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
46 |
47 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
48 |
49 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
50 |
--------------------------------------------------------------------------------