-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPXGeocodingManager.m
More file actions
162 lines (137 loc) · 5.3 KB
/
PXGeocodingManager.m
File metadata and controls
162 lines (137 loc) · 5.3 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
//
// PXGeocodingManager.m
//
// Created by Daniel Blakemore on 7/16/14.
//
// Copyright (c) 2015 Pixio
//
// 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:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// 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.
//
#import "PXGeocodingManager.h"
#define PXGeocoderHaveNewSignal 1
#define PXGeocoderNoNewSignal 0
#define RetryCount 10
@implementation PXGeocodingManager
{
NSMutableArray * _queue;
NSMutableArray * _thunks;
NSConditionLock * _queueLock;
NSConditionLock * _newSignal;
NSThread * _processingThread;
}
+ (instancetype)sharedManger
{
static PXGeocodingManager * manager = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
manager = [[PXGeocodingManager alloc] init];
});
return manager;
}
- (id) init
{
self = [super init];
if (self) {
_queue = [NSMutableArray array];
_thunks = [NSMutableArray array];
_queueLock = [[NSConditionLock alloc] init];
_newSignal = [[NSConditionLock alloc] initWithCondition:PXGeocoderNoNewSignal];
_processingThread = [[NSThread alloc] initWithTarget:self selector:@selector(processThem) object:nil];
[_processingThread start];
}
return self;
}
- (void)geocodeAddressString:(NSString*)addressString callback:(void(^)(CLPlacemark * placemark, NSInteger errorCode))callback
{
NSAssert(callback, @"callback cannot be nil");
[_queueLock lock];
[_queue addObject:addressString];
[_thunks addObject:callback];
[_queueLock unlock];
// signal new things to do
[_newSignal lock];
[_newSignal unlockWithCondition:PXGeocoderHaveNewSignal];
}
- (CLPlacemark*)synchronatedGeocodeAddress:(NSString*)address errorCode:(NSInteger*)errorCode
{
__block CLPlacemark * theThingThatGoesOut = nil;
NSConditionLock * lock = [[NSConditionLock alloc] initWithCondition:0];
CLGeocoder * geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:address completionHandler:^(NSArray* placemarks, NSError* error) {
if (error) {
// NSLog(@"Geocoding Error: %@", [error description]);
*errorCode = [error code];
} else {
if ([placemarks count]) {
theThingThatGoesOut = [placemarks objectAtIndex:0];
}
}
[lock lock];
[lock unlockWithCondition:1];
}];
[lock lockWhenCondition:1];
[lock unlock]; // cleanup
return theThingThatGoesOut;
}
- (void)processThem
{
while (TRUE) {
// see if queue has things
[_queueLock lock];
NSString * address = nil;
void(^callback)(CLPlacemark * placemark, NSInteger errorCode) = nil;
if ([_queue count]) {
// need to do things, pop thing to do
address = [_queue objectAtIndex:0];
callback = [_thunks objectAtIndex:0];
[_queue removeObjectAtIndex:0];
[_thunks removeObjectAtIndex:0];
}
[_queueLock unlock];
if (!address) {
// nothing to do, wait until something to do
[_newSignal lock];
[_newSignal unlockWithCondition:PXGeocoderNoNewSignal];
[_newSignal lockWhenCondition:PXGeocoderHaveNewSignal];
[_newSignal unlock];
} else {
// process thing
CLPlacemark * placemark = nil;
NSInteger sleepIncrementBaseValue = 150000; // µs
NSInteger sleepIncrementMultiplier = 1;
NSInteger errorCode = kCLErrorNetwork;
NSInteger sleepTime = sleepIncrementBaseValue * sleepIncrementMultiplier++;
usleep((unsigned int)sleepTime);
while (!(placemark = [self synchronatedGeocodeAddress:address errorCode:&errorCode])) {
// failed again, need to sleep more
sleepTime += sleepIncrementBaseValue * sleepIncrementMultiplier++;
if (sleepIncrementMultiplier > RetryCount || errorCode != kCLErrorNetwork) {
// give up on this one
break;
}
usleep((unsigned int)sleepTime);
}
dispatch_async(dispatch_get_main_queue(), ^{
// call back on main thread
callback(placemark, errorCode);
});
}
}
}
@end