Fix some threading issue.

This commit is contained in:
Damiano Galassi committed 2016-10-01 12:54:03 +02:00
1 parent d34a2876db
commit 9224a6e0db
4 files changed
+30 -16

No files matched your search

-1
View File
@@ -56,4 +56,3 @@ extern NSString *SBQueueCancelledNotification;
@end @end
NS_ASSUME_NONNULL_END NS_ASSUME_NONNULL_END
+25 -11
View File
@@ -15,21 +15,21 @@ NSString *SBQueueFailedNotification = @"SBQueueFailedNotification";
NSString *SBQueueCancelledNotification = @"SBQueueCancelledNotification"; NSString *SBQueueCancelledNotification = @"SBQueueCancelledNotification";
@interface SBQueue () @interface SBQueue ()
{
IOPMAssertionID _assertionID;
IOReturn _io_success;
}
@property (atomic) SBQueueStatus status; @property (atomic) SBQueueStatus status;
@property (nonatomic, copy) NSURL *URL; @property (nonatomic, copy) NSURL *URL;
@property (nonatomic) NSMutableArray *items; @property (nonatomic) NSMutableArray *items;
@property (nonatomic) NSUInteger currentIndex;
@property (atomic) SBQueueItem *currentItem; @property (atomic) SBQueueItem *currentItem;
@property (atomic) NSUInteger currentIndex;
@property (atomic) BOOL cancelled; @property (atomic) BOOL cancelled;
@property (nonatomic) IOPMAssertionID assertionID;
@property (nonatomic) IOReturn io_success;
@property (nonatomic) dispatch_queue_t workQueue; @property (nonatomic) dispatch_queue_t workQueue;
@property (nonatomic) dispatch_queue_t saveQueue;
@end @end
@@ -39,12 +39,14 @@ NSString *SBQueueCancelledNotification = @"SBQueueCancelledNotification";
self = [super init]; self = [super init];
if (self) { if (self) {
_workQueue = dispatch_queue_create("org.subler.WorkQueue", NULL); _workQueue = dispatch_queue_create("org.subler.WorkQueue", NULL);
_saveQueue = dispatch_queue_create("org.subler.SaveQueue", NULL);
_URL = [queueURL copy]; _URL = [queueURL copy];
if ([[NSFileManager defaultManager] fileExistsAtPath:queueURL.path]) { if ([[NSFileManager defaultManager] fileExistsAtPath:queueURL.path]) {
@try { @try {
_items = [NSKeyedUnarchiver unarchiveObjectWithFile:queueURL.path]; _items = [NSKeyedUnarchiver unarchiveObjectWithFile:queueURL.path];
} @catch (NSException *exception) { }
@catch (NSException *exception) {
[[NSFileManager defaultManager] removeItemAtURL:queueURL error:nil]; [[NSFileManager defaultManager] removeItemAtURL:queueURL error:nil];
_items = nil; _items = nil;
} }
@@ -64,10 +66,13 @@ NSString *SBQueueCancelledNotification = @"SBQueueCancelledNotification";
} }
- (BOOL)saveQueueToDisk { - (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 { - (SBQueueItem *)firstItemInQueue {
__block SBQueueItem *firstItem = nil; __block SBQueueItem *firstItem = nil;
dispatch_sync(dispatch_get_main_queue(), ^{ dispatch_sync(dispatch_get_main_queue(), ^{
@@ -82,6 +87,14 @@ NSString *SBQueueCancelledNotification = @"SBQueueCancelledNotification";
return firstItem; 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 #pragma mark - item management
- (void)addItem:(SBQueueItem *)item { - (void)addItem:(SBQueueItem *)item {
@@ -89,7 +102,7 @@ NSString *SBQueueCancelledNotification = @"SBQueueCancelledNotification";
} }
- (NSUInteger)count { - (NSUInteger)count {
return (self.items).count; return self.items.count;
} }
- (NSUInteger)readyCount { - (NSUInteger)readyCount {
@@ -193,7 +206,7 @@ NSString *SBQueueCancelledNotification = @"SBQueueCancelledNotification";
break; break;
} }
self.currentIndex = [self.items indexOfObject:self.currentItem]; self.currentIndex = [self indexOfCurrentItem];
self.currentItem.status = SBQueueItemStatusWorking; self.currentItem.status = SBQueueItemStatusWorking;
self.currentItem.delegate = self; self.currentItem.delegate = self;
@@ -213,7 +226,8 @@ NSString *SBQueueCancelledNotification = @"SBQueueCancelledNotification";
if (noErr) { if (noErr) {
self.currentItem.status = SBQueueItemStatusCompleted; self.currentItem.status = SBQueueItemStatusCompleted;
completedCount += 1; completedCount += 1;
} else { }
else {
self.currentItem.status = SBQueueItemStatusFailed; self.currentItem.status = SBQueueItemStatusFailed;
failedCount += 1; failedCount += 1;
[self handleSBStatusFailed:outError]; [self handleSBStatusFailed:outError];
+3 -2
View File
@@ -325,10 +325,11 @@
[self didChangeValueForKey:@"fileURL"]; [self didChangeValueForKey:@"fileURL"];
if (!self.cancelled) { 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 // We have an existing mp4 file, update it
noErr = [self.mp4File updateMP4FileWithOptions:self.attributes error:outError]; noErr = [self.mp4File updateMP4FileWithOptions:self.attributes error:outError];
} else { }
else {
// Write the new file to disk // Write the new file to disk
noErr = [self.mp4File writeToUrl:self.destURL noErr = [self.mp4File writeToUrl:self.destURL
options:self.attributes options:self.attributes
+2 -2
View File
@@ -3,7 +3,7 @@
archiveVersion = 1; archiveVersion = 1;
classes = { classes = {
}; };
objectVersion = 46; objectVersion = 48;
objects = { objects = {
/* Begin PBXBuildFile section */ /* Begin PBXBuildFile section */
@@ -739,7 +739,7 @@
}; };
}; };
buildConfigurationList = C05733CB08A9546B00998B17 /* Build configuration list for PBXProject "Subler" */; buildConfigurationList = C05733CB08A9546B00998B17 /* Build configuration list for PBXProject "Subler" */;
compatibilityVersion = "Xcode 3.2"; compatibilityVersion = "Xcode 8.0";
developmentRegion = English; developmentRegion = English;
hasScannedForEncodings = 1; hasScannedForEncodings = 1;
knownRegions = ( knownRegions = (