From 9224a6e0db608be63428449844e87cb15007dec3 Mon Sep 17 00:00:00 2001 From: Damiano Galassi Date: Sat, 1 Oct 2016 12:54:03 +0200 Subject: [PATCH] Fix some threading issue. --- Classes/SBQueue.h | 1 - Classes/SBQueue.m | 36 ++++++++++++++++++++++---------- Classes/SBQueueItem.m | 5 +++-- Subler.xcodeproj/project.pbxproj | 4 ++-- 4 files changed, 30 insertions(+), 16 deletions(-) diff --git a/Classes/SBQueue.h b/Classes/SBQueue.h index db5b490..b6f8718 100644 --- a/Classes/SBQueue.h +++ b/Classes/SBQueue.h @@ -56,4 +56,3 @@ extern NSString *SBQueueCancelledNotification; @end NS_ASSUME_NONNULL_END - diff --git a/Classes/SBQueue.m b/Classes/SBQueue.m index db4fc66..b1cad39 100644 --- a/Classes/SBQueue.m +++ b/Classes/SBQueue.m @@ -15,21 +15,21 @@ NSString *SBQueueFailedNotification = @"SBQueueFailedNotification"; NSString *SBQueueCancelledNotification = @"SBQueueCancelledNotification"; @interface SBQueue () -{ - IOPMAssertionID _assertionID; - IOReturn _io_success; -} @property (atomic) SBQueueStatus status; @property (nonatomic, copy) NSURL *URL; @property (nonatomic) NSMutableArray *items; +@property (nonatomic) NSUInteger currentIndex; @property (atomic) SBQueueItem *currentItem; -@property (atomic) NSUInteger currentIndex; @property (atomic) BOOL cancelled; +@property (nonatomic) IOPMAssertionID assertionID; +@property (nonatomic) IOReturn io_success; + @property (nonatomic) dispatch_queue_t workQueue; +@property (nonatomic) dispatch_queue_t saveQueue; @end @@ -39,12 +39,14 @@ NSString *SBQueueCancelledNotification = @"SBQueueCancelledNotification"; self = [super init]; if (self) { _workQueue = dispatch_queue_create("org.subler.WorkQueue", NULL); + _saveQueue = dispatch_queue_create("org.subler.SaveQueue", NULL); _URL = [queueURL copy]; if ([[NSFileManager defaultManager] fileExistsAtPath:queueURL.path]) { @try { _items = [NSKeyedUnarchiver unarchiveObjectWithFile:queueURL.path]; - } @catch (NSException *exception) { + } + @catch (NSException *exception) { [[NSFileManager defaultManager] removeItemAtURL:queueURL error:nil]; _items = nil; } @@ -64,10 +66,13 @@ NSString *SBQueueCancelledNotification = @"SBQueueCancelledNotification"; } - (BOOL)saveQueueToDisk { - return [NSKeyedArchiver archiveRootObject:self.items toFile:(self.URL).path]; + __block BOOL result; + dispatch_sync(self.saveQueue, ^{ + result = [NSKeyedArchiver archiveRootObject:self.items toFile:self.URL.path]; + }); + return result; } - - (SBQueueItem *)firstItemInQueue { __block SBQueueItem *firstItem = nil; dispatch_sync(dispatch_get_main_queue(), ^{ @@ -82,6 +87,14 @@ NSString *SBQueueCancelledNotification = @"SBQueueCancelledNotification"; return firstItem; } +- (NSUInteger)indexOfCurrentItem { + __block NSUInteger index; + dispatch_sync(dispatch_get_main_queue(), ^{ + index = [self.items indexOfObject:self.currentItem]; + }); + return index; +} + #pragma mark - item management - (void)addItem:(SBQueueItem *)item { @@ -89,7 +102,7 @@ NSString *SBQueueCancelledNotification = @"SBQueueCancelledNotification"; } - (NSUInteger)count { - return (self.items).count; + return self.items.count; } - (NSUInteger)readyCount { @@ -193,7 +206,7 @@ NSString *SBQueueCancelledNotification = @"SBQueueCancelledNotification"; break; } - self.currentIndex = [self.items indexOfObject:self.currentItem]; + self.currentIndex = [self indexOfCurrentItem]; self.currentItem.status = SBQueueItemStatusWorking; self.currentItem.delegate = self; @@ -213,7 +226,8 @@ NSString *SBQueueCancelledNotification = @"SBQueueCancelledNotification"; if (noErr) { self.currentItem.status = SBQueueItemStatusCompleted; completedCount += 1; - } else { + } + else { self.currentItem.status = SBQueueItemStatusFailed; failedCount += 1; [self handleSBStatusFailed:outError]; diff --git a/Classes/SBQueueItem.m b/Classes/SBQueueItem.m index d865dbe..2aca1be 100755 --- a/Classes/SBQueueItem.m +++ b/Classes/SBQueueItem.m @@ -325,10 +325,11 @@ [self didChangeValueForKey:@"fileURL"]; if (!self.cancelled) { - if ([self.fileURL isEqualTo:self.destURL] && (self.mp4File).hasFileRepresentation) { + if ([self.fileURL isEqualTo:self.destURL] && self.mp4File.hasFileRepresentation) { // We have an existing mp4 file, update it noErr = [self.mp4File updateMP4FileWithOptions:self.attributes error:outError]; - } else { + } + else { // Write the new file to disk noErr = [self.mp4File writeToUrl:self.destURL options:self.attributes diff --git a/Subler.xcodeproj/project.pbxproj b/Subler.xcodeproj/project.pbxproj index e8d7d06..b5a373e 100644 --- a/Subler.xcodeproj/project.pbxproj +++ b/Subler.xcodeproj/project.pbxproj @@ -3,7 +3,7 @@ archiveVersion = 1; classes = { }; - objectVersion = 46; + objectVersion = 48; objects = { /* Begin PBXBuildFile section */ @@ -739,7 +739,7 @@ }; }; buildConfigurationList = C05733CB08A9546B00998B17 /* Build configuration list for PBXProject "Subler" */; - compatibilityVersion = "Xcode 3.2"; + compatibilityVersion = "Xcode 8.0"; developmentRegion = English; hasScannedForEncodings = 1; knownRegions = (