forked from mxcl/PromiseKit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPromiseKit+CoreLocation.m
55 lines (40 loc) · 1.21 KB
/
PromiseKit+CoreLocation.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
@import CoreLocation.CLLocationManagerDelegate;
#import "Private/macros.m"
#import "PromiseKit+CoreLocation.h"
#import "PromiseKit/Deferred.h"
#import "PromiseKit/Promise.h"
@interface PMKLocationManager : CLLocationManager <CLLocationManagerDelegate>
@end
@implementation PMKLocationManager {
Deferred *deferred;
}
- (id)init {
self = [super init];
deferred = [Deferred new];
return self;
}
- (Promise *)promise {
return deferred.promise;
}
#define PMKLocationManagerCleanup() \
[manager stopUpdatingLocation]; \
self.delegate = nil; \
__anti_arc_release(self);
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
[deferred resolve:locations.count == 1 ? locations[0] : locations];
PMKLocationManagerCleanup();
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
[deferred reject:error];
PMKLocationManagerCleanup();
}
@end
@implementation CLLocationManager (PromiseKit)
+ (Promise *)promise {
PMKLocationManager *manager = [PMKLocationManager new];
manager.delegate = manager;
[manager startUpdatingLocation];
__anti_arc_retain(manager);
return manager.promise;
}
@end