• Stars
    star
    971
  • Rank 47,111 (Top 1.0 %)
  • Language
    Objective-C
  • License
    MIT License
  • Created over 11 years ago
  • Updated almost 8 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Thread-safe NSTimer alternative that doesn't retain the target and supports being used with GCD queues.

MSWeakTimer

Description

Thread-safe NSTimer drop-in alternative that doesn't retain the target and supports being used with GCD queues.

Motivation

The first motivation for this class was to have a type of timer that objects could own and retain, without this creating a retain cycle ( like NSTimer causes, since it retains its target ). This way you can just release the timer in the -dealloc method of the object class that owns the timer.

The other problem when using NSTimer is this note on the documentation:

Special Considerations

You must send this message from the thread on which the timer was installed. If you send this message from another thread, the input source associated with the timer may not be removed from its run loop, which could prevent the thread from exiting properly.

More often than not, an object needs to create a timer and invalidate it when a certain event occurs. However, doing this when that object works with a private GCD queue gets tricky. This timer object is thread safe and doesn't have the notion of run loop, so it can be used with GCD queues and installed / invalidated from any thread or queue.

Related Stackoverflow question.

Implementation

The implementation of MSWeakTimer was reviewed and validated by a libdispatch (GCD) engineer at the WWDC 2013 Core OS Lab.

How to Use

Create an MSWeakTimer object with this class method:

+ (MSWeakTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)timeInterval
                                         target:(id)target
                                       selector:(SEL)selector
                                       userInfo:(id)userInfo
                                        repeats:(BOOL)repeats
                                  dispatchQueue:(dispatch_queue_t)dispatchQueue;

Check out the code in the sample app.

Installation

Just add this line to your Podfile:

pod 'MSWeakTimer', '~> 1.1.0'
  • Manually:

Simply add the files MSWeakTimer.h and MSWeakTimer.m to your project.

Compatibility

  • Requires ARC. If you want to use it in a project without ARC, mark MSWeakTimer.m with the linker flag -fobjc-arc.
  • Supports iOS iOS5+ and Mac OSX 10.7+.

License

MSWeakTimer is available under the MIT license. See the LICENSE file for more info.

More Repositories

1

MSLeakHunter

Series of tools to help you find and debug objects that are leaking / over-released in your iOS apps.
Objective-C
366
star
2

MSCachedAsyncViewDrawing

Helper class that allows you to draw views (a)synchronously to a UIImage with caching for great performance.
Objective-C
277
star
3

MSVCLeakHunter

Simple and easy tool to discover View Controllers that are not being deallocated when you expect them to.
Objective-C
116
star
4

MSPageViewController

Create UIPageViewControllers using only storyboards
Objective-C
89
star
5

MSActiveConfig

Remote configuration and A/B Testing framework for iOS
Objective-C
79
star
6

Zinc

Asset distribution system for mobile clients.
Python
40
star
7

MSEmojiChecksumCalculator

Calculate checksums that are human readable and fun!
Objective-C
29
star
8

MSSidebarController

Fully customizable sidebar controller
Objective-C
27
star
9

MSSpec

Kiwi Spec with support to inject mocks using Objection
Objective-C
22
star
10

Zinc-ObjC

Objective-C client for Zinc asset distribution system
Objective-C
17
star
11

provtool

Command line utility for working with Provisioning Profiles
Python
12
star
12

UIImage-MSImageNamed568hSupport

Method for UIImage to be able to load images with the -568h prefix on the iPhone5.
Objective-C
12
star
13

django-versionfield

A DB Independent Custom Field for storing Version numbers for fast indexing
Python
8
star
14

MSWeakMutableSet

Behaves exactly like an NSMutableSet, but doesn't retain the objects.
Objective-C
8
star
15

penmanship

A simple Gradle plug-in that makes it easy to convert Markdown documents into native Android layout XML files.
Java
7
star
16

JavaZinc

Zinc client in Java
Java
6
star
17

pngnq-mac

Mac OSX build of the pngnq image quantizer with some command line modifications
C
5
star
18

iso8601parser

This is a "fork" of https://github.com/square/iso8601parser but the source has disappeared
Objective-C
3
star
19

django-piston

This is my fork of django-piston by jespern that fixes the following bugs: #171, #84, #169
Python
1
star
20

mindsnacks.github.com

1
star