@@ -187,13 +187,12 @@ - (void)persistEventually {
187187
188188- (void )persistImmediately {
189189 [self .writeTimer invalidate ];
190-
191190 [self persistToDisk ];
192191}
193192
194193- (void )persistToDisk {
195194 NSArray *requestsToPersist = [self .queue copy ];
196- dispatch_async (self.asyncQueue , ^{
195+ dispatch_async (self.asyncQueue , ^ {
197196 @try {
198197 NSMutableArray *encodedRequests = [[NSMutableArray alloc ] init ];
199198 for (BNCServerRequest *req in requestsToPersist) {
@@ -205,13 +204,26 @@ - (void)persistToDisk {
205204 NSData *encodedReq = [NSKeyedArchiver archivedDataWithRootObject: req];
206205 [encodedRequests addObject: encodedReq];
207206 }
208-
209- if (![NSKeyedArchiver archiveRootObject: encodedRequests toFile: [self queueFile ]]) {
210- [[BNCPreferenceHelper preferenceHelper ] logWarning: @" Failed to persist queue to disk" ];
207+
208+ NSData *data = [NSKeyedArchiver archivedDataWithRootObject: encodedRequests];
209+ if (!data) {
210+ [[BNCPreferenceHelper preferenceHelper ]
211+ logWarning: @" Cannot create archive data." ];
212+ return ;
213+ }
214+ NSError *error = nil ;
215+ [data writeToURL: self .class.URLForQueueFile
216+ options: NSDataWritingAtomic error: &error];
217+ if (error) {
218+ [[BNCPreferenceHelper preferenceHelper ] logWarning:
219+ [NSString stringWithFormat: @" Failed to persist queue to disk: %@ " , error]];
211220 }
212221 }
213222 @catch (NSException *exception) {
214- NSString *warningMessage = [NSString stringWithFormat: @" An exception occurred while attempting to save the queue. Exception information:\n\n %@ " , [self exceptionString: exception]];
223+ NSString *warningMessage =
224+ [NSString stringWithFormat:
225+ @" An exception occurred while attempting to save the queue. Exception information:\n\n %@ " ,
226+ [self exceptionString: exception]];
215227 [[BNCPreferenceHelper preferenceHelper ] logWarning: warningMessage];
216228 }
217229 });
@@ -223,10 +235,19 @@ - (void)retrieve {
223235
224236 // Capture exception while loading the queue file
225237 @try {
226- encodedRequests = [NSKeyedUnarchiver unarchiveObjectWithFile: [self queueFile ]];
238+ NSError *error = nil ;
239+ NSData *data = [NSData dataWithContentsOfURL: self .class.URLForQueueFile options: 0 error: &error];
240+ if (error || !data)
241+ [[BNCPreferenceHelper preferenceHelper ] logWarning:
242+ [NSString stringWithFormat: @" Error loading network queue: %@ ." , error]];
243+ else
244+ encodedRequests = [NSKeyedUnarchiver unarchiveObjectWithData: data];
227245 }
228246 @catch (NSException *exception) {
229- NSString *warningMessage = [NSString stringWithFormat: @" An exception occurred while attempting to load the queue file, proceeding without requests. Exception information:\n\n %@ " , [self exceptionString: exception]];
247+ NSString *warningMessage =
248+ [NSString stringWithFormat:
249+ @" An exception occurred while attempting to load the queue file, proceeding without requests. Exception information:\n\n %@ " ,
250+ [self exceptionString: exception]];
230251 [[BNCPreferenceHelper preferenceHelper ] logWarning: warningMessage];
231252 self.queue = queue;
232253 return ;
@@ -265,8 +286,42 @@ - (NSString *)exceptionString:(NSException *)exception {
265286 return [NSString stringWithFormat: @" Name: %@ \n Reason: %@ \n Stack:\n\t %@ \n\n " , exception.name, exception.reason, [exception.callStackSymbols componentsJoinedByString: @" \n\t " ]];
266287}
267288
268- - (NSString *)queueFile {
269- return [[NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES ) firstObject ] stringByAppendingPathComponent: BRANCH_QUEUE_FILE];
289+ + (NSString *)queueFile_deprecated {
290+ NSString *path =
291+ [[NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES )
292+ firstObject ]
293+ stringByAppendingPathComponent: BRANCH_QUEUE_FILE];
294+ return path;
295+ }
296+
297+ + (NSURL *) URLForQueueFile {
298+ NSURL *URL = [BNCPreferenceHelper URLForBranchDirectory ];
299+ URL = [URL URLByAppendingPathComponent: BRANCH_QUEUE_FILE];
300+ return URL;
301+ }
302+
303+ + (void ) moveOldQueueFile {
304+ NSURL *oldURL = [NSURL fileURLWithPath: self .queueFile_deprecated];
305+ NSURL *newURL = [self URLForQueueFile ];
306+ NSError *error = nil ;
307+ [[NSFileManager defaultManager ]
308+ moveItemAtURL: oldURL
309+ toURL: newURL
310+ error: &error];
311+
312+ if (error && error.code != NSFileNoSuchFileError) {
313+ if (error.code == NSFileWriteFileExistsError) {
314+ [[NSFileManager defaultManager ]
315+ removeItemAtURL: oldURL
316+ error: &error];
317+ } else {
318+ NSLog (@" Error moving queue file: %@ ." , error);
319+ }
320+ }
321+ }
322+
323+ + (void ) initialize {
324+ [self moveOldQueueFile ];
270325}
271326
272327#pragma mark - Singleton method
@@ -275,12 +330,15 @@ + (id)getInstance {
275330 static BNCServerRequestQueue *sharedQueue = nil ;
276331 static dispatch_once_t onceToken;
277332
278- dispatch_once (&onceToken, ^{
333+ dispatch_once (&onceToken, ^ {
279334 sharedQueue = [[BNCServerRequestQueue alloc ] init ];
280335 [sharedQueue retrieve ];
281- [[BNCPreferenceHelper preferenceHelper ] log: FILE_NAME line: LINE_NUM message: @" Retrieved from Persist: %@ " , sharedQueue];
336+ [[BNCPreferenceHelper preferenceHelper ]
337+ log: FILE_NAME
338+ line: LINE_NUM
339+ message: @" Retrieved from Persist: %@ " , sharedQueue];
282340 });
283-
341+
284342 return sharedQueue;
285343}
286344
0 commit comments