Commits

José Manuel Sánchez Peñarroja committed 5652c12

Improved performance of progress, but some things still not working

  • Participants
  • Parent commits 403b885

Comments (0)

Files changed (5)

bmf/shared/base/BMFMutableWeakArray.m

 		}
 	}
 	
-	if (objectsToDelete.count>0) DDLogInfo(@"Deleting some objects from mutablearray: %@",objectsToDelete);
+//	if (objectsToDelete.count>0) DDLogInfo(@"Deleting some objects from mutablearray: %@",objectsToDelete);
 	[self.array removeObjectsInArray:objectsToDelete];
 }
 

bmf/shared/data/BMFProgress.h

 
 @property (nonatomic, readonly) BMFMutableWeakArray *children;
 
+/// Block to perform some action when progress changes
+@property (nonatomic, copy) BMFActionBlock changedBlock;
+
 /// This key is used to identify the task. The estimated time will be averaged based on it
 @property (nonatomic, strong) NSString *key;
 

bmf/shared/data/BMFProgress.m

 
 @property (nonatomic,assign) BOOL running;
 @property (nonatomic, strong) BMFMutableWeakArray *children;
+@property (nonatomic, copy) BMFActionBlock progressChangedBlock;
 
 @end
 
 		self.totalUnitCount = 1;
 		self.estimatedTime = 1; // 1 second by default
 
-		dispatch_sync(serialQueue, ^{
-			self.children = [BMFMutableWeakArray new];
+//		dispatch_sync(serialQueue, ^{
+//			_children = [BMFMutableWeakArray new];
 			
-			_childrenEstimatedTimeSignal = [RACSubject subject];
+			/*_childrenEstimatedTimeSignal = [RACSubject subject];
 			_childrenFractionCompletedSignal = [RACSubject subject];
 			_childrenRunningSignal = [RACSubject subject];
 			_childrenMessageSignal = [RACSubject subject];
 			@weakify(self);
 			[[_childrenEstimatedTimeSignal flatten] subscribeNext:^(id x) {
 				@strongify(self);
-				NSTimeInterval total = 0;
+				__block NSTimeInterval total = 0;
 				
 				__block NSArray *children = nil;
 				dispatch_sync(serialQueue, ^{
-					children = [self.children copy];
+//					children = [self.children copy];
+					for (BMFProgress *child in children) {
+						total += child.estimatedTime;
+					}
 				});
 				
-				for (BMFProgress *child in children) {
-					total += child.estimatedTime;
-				}
+//				for (BMFProgress *child in children) {
+//					total += child.estimatedTime;
+//				}
 				
 				if (_estimatedTime!=total) self.estimatedTime = total;
-			}];
+			}];*/
 			
-			[[_childrenFractionCompletedSignal flatten] subscribeNext:^(id x) {
-				@strongify(self);
-				int64_t total = 0;
-				int64_t completed = 0;
-				
-				__block NSArray *children = nil;
-				dispatch_sync(serialQueue, ^{
-					children = [self.children copy];
-				});
-				
-				for (BMFProgress *child in children) {
-					CGFloat childTotal = child.estimatedTime*1000;
-					
-					total += childTotal;
-					completed += child.fractionCompleted*childTotal;
-				}
-				
-				if (_totalUnitCount!=total)	self.totalUnitCount = total;
-				if (_completedUnitCount!=completed) self.completedUnitCount = completed;
-				
-			}];
+//			[[_childrenFractionCompletedSignal flatten] subscribeNext:^(id x) {
+//				@strongify(self);
+//				__block int64_t total = 0;
+//				__block int64_t completed = 0;
+//				
+//				__block NSArray *children = nil;
+//				dispatch_sync(serialQueue, ^{
+////					children = [self.children copy];
+//					for (BMFProgress *child in children) {
+//						CGFloat childTotal = child.estimatedTime*1000;
+//						
+//						total += childTotal;
+//						completed += child.fractionCompleted*childTotal;
+//					}
+//				});
+//				
+//				
+//				if (_totalUnitCount!=total)	self.totalUnitCount = total;
+//				if (_completedUnitCount!=completed) self.completedUnitCount = completed;
+//				
+//			}];
 			
-			[[_childrenRunningSignal flatten] subscribeNext:^(id x) {
+			/*[[_childrenRunningSignal flatten] subscribeNext:^(id x) {
 				@strongify(self);
-				BOOL running = NO;
+				__block BOOL running = NO;
 				
 				__block NSArray *children = nil;
 				dispatch_sync(serialQueue, ^{
-					children = [self.children copy];
-				});
-				
-				for (BMFProgress *child in children) {
-					if (child.running) {
-						running = YES;
-						break;
+//					children = [self.children copy];
+					for (BMFProgress *child in children) {
+						if (child.running) {
+							running = YES;
+							break;
+						}
 					}
-				}
+				});
 				
 				if (_running!=running) self.running = running;
 			}];
 				
 				__block NSArray *children = nil;
 				dispatch_sync(serialQueue, ^{
-					children = [self.children copy];
+//					children = [self.children copy];
+					for (BMFProgress *child in children) {
+						if (child.running && child.progressMessage.length>0) {
+							self.progressMessage = child.progressMessage;
+							break;
+						}
+					}
 				});
 				
-				for (BMFProgress *child in children) {
-					if (child.running && child.progressMessage.length>0) {
-						self.progressMessage = child.progressMessage;
-						break;
-					}
-				}
-			}];
-		});
+				
+			}];*/
+//		});
     }
     return self;
 }
 
+- (BMFMutableWeakArray *) children {
+	dispatch_sync(serialQueue, ^{
+		if (!_children) _children = [BMFMutableWeakArray new];
+	});
+	
+	return _children;
+}
+
+- (void) updateValuesFromChildren {
+	__block int64_t total = 0;
+	__block int64_t completed = 0;
+	__block int64_t estimated = 0;
+	
+	dispatch_sync(serialQueue, ^{
+		_progressMessage = nil;
+		_running = NO;
+		
+		for (BMFProgress *child in _children) {
+			if (child.running) {
+				_running = YES;
+				
+				NSString *childMessage = child.progressMessage;
+				if (childMessage.length>0 && !_progressMessage) {
+					_progressMessage = childMessage;
+				}
+			}
+			
+			int64_t childEstimated = child.estimatedTime;
+			CGFloat childTotal = childEstimated*1000;
+			
+			total += childTotal;
+			completed += child.fractionCompleted*childTotal;
+			estimated += childEstimated;
+		}
+		
+		_totalUnitCount = total;
+		_completedUnitCount = completed;
+		_estimatedTime = estimated;
+
+	});
+		
+	if (self.changedBlock) self.changedBlock(self);
+}
+
 - (int64_t) totalUnitCount {
 	__block int64_t result = 0;
 	
 	return MAX(MIN(result,1),0);
 }
 
+- (void) setProgressMessage:(NSString *)progressMessage {
+	dispatch_sync(serialQueue, ^{
+		_progressMessage = [progressMessage copy];
+	});
+	
+	if (self.progressChangedBlock) self.progressChangedBlock(self);
+	if (self.changedBlock) self.changedBlock(self);
+}
+
 - (void) clear {
 	self.completedUnitCount = 0;
 	self.progressMessage = nil;
 	dispatch_sync(serialQueue, ^{
 		_running = running;
 	});
+	
+	if (self.progressChangedBlock) self.progressChangedBlock(self);
+	if (self.changedBlock) self.changedBlock(self);
 }
 
 - (NSTimeInterval) estimatedTime {
 	dispatch_sync(serialQueue, ^{
 		_estimatedTime = estimatedTime;
 	});
+
+	if (self.progressChangedBlock) self.progressChangedBlock(self);
+	if (self.changedBlock) self.changedBlock(self);
 }
 
 - (int64_t) completedUnitCount {
 	});
 	[self didChangeValueForKey:@"fractionCompleted"];
 	[self didChangeValueForKey:@"completedUnitCount"];
+	
+	if (self.progressChangedBlock) self.progressChangedBlock(self);
+	if (self.changedBlock) self.changedBlock(self);
 }
 
 - (NSError *) failedError {
 	});
 	[self didChangeValueForKey:@"progressMessage"];
 	[self didChangeValueForKey:@"failedError"];
+	
+	if (self.progressChangedBlock) self.progressChangedBlock(self);
+	if (self.changedBlock) self.changedBlock(self);
 }
 
 - (void) addChild:(BMFProgress *) child {
 		return;	
 	}
 	
-	dispatch_async(serialQueue, ^{
+	dispatch_sync(serialQueue, ^{
 		[self.children addObject:child];
 		
-		[_childrenEstimatedTimeSignal sendNext:RACObserve(child, estimatedTime)];
-		[_childrenFractionCompletedSignal sendNext:RACObserve(child, fractionCompleted)];
-		[_childrenRunningSignal sendNext:RACObserve(child, running)];
-		[_childrenMessageSignal sendNext:RACObserve(child, progressMessage)];
+//		[_childrenEstimatedTimeSignal sendNext:RACObserve(child, estimatedTime)];
+//		[_childrenFractionCompletedSignal sendNext:RACObserve(child, fractionCompleted)];
+//		[_childrenRunningSignal sendNext:RACObserve(child, running)];
+//		[_childrenMessageSignal sendNext:RACObserve(child, progressMessage)];
 	});
+	
+	child.progressChangedBlock = ^(BMFProgress *child) {
+		[self updateValuesFromChildren];
+	};
 }
 
 /*- (void) update {

bmf/shared/data/loaders/BMFAFURLSessionLoader.m

 	
 	@autoreleasepool {
 		
-		[_progress start:self.url.absoluteString];
-		
 		self.finalUrl = self.url;
 		
 		_progress.progressMessage = BMFLocalized(@"Loading data", nil);
 			}
 		}
 		
+		[_progress start:self.url.absoluteString];
+		
 		DDLogDebug(@"Load url: %@ params: %@",self.url.absoluteString,self.parameters);
 		
 		/*AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:self.configuration];

bmf/shared/views/BMFProgressUI.m

     if (self) {
 		_progress = [BMFProgress new];
 		
-		@weakify(self);
-		[[RACObserve(self, progress.running) deliverOn:[RACScheduler mainThreadScheduler]] subscribeNext:^(NSNumber *runningNumber) {
-			@strongify(self);
-			[self updateRunning:runningNumber.boolValue];
-		}];
-		[[RACObserve(self, progress.fractionCompleted) deliverOn:[RACScheduler mainThreadScheduler]] subscribeNext:^(NSNumber *completed) {
-			@strongify(self);
-			[self updateProgress:[self displayedProgressWithFractionCompleted:completed.floatValue]];
-		}];
+		_progress.changedBlock = ^(BMFProgress *progress) {
+			dispatch_async(dispatch_get_main_queue(), ^{
+				[self updateRunning:progress.running];
+				[self updateProgress:[self displayedProgressWithFractionCompleted:progress.fractionCompleted]];				
+			});
+		};
+		
+//		@weakify(self);
+//		[[RACObserve(self, progress.running) deliverOn:[RACScheduler mainThreadScheduler]] subscribeNext:^(NSNumber *runningNumber) {
+//			@strongify(self);
+//			[self updateRunning:runningNumber.boolValue];
+//		}];
+//		[[RACObserve(self, progress.fractionCompleted) deliverOn:[RACScheduler mainThreadScheduler]] subscribeNext:^(NSNumber *completed) {
+//			@strongify(self);
+//			[self updateProgress:[self displayedProgressWithFractionCompleted:completed.floatValue]];
+//		}];
     }
     return self;
 }