Reputation: 8800
Following is my Method for uploading video on background thread reading from plist file .
Now what i need is once they read all the entry from plist and done execution of first block i want to check in completion block that is there any new entry came in plist file..and if not just call startThreadForUpload
after few time.so can any one suggest me how can i do that? right now i just calling same method in completion block so it keeps on runnning...
-(void)startThreadForUpload{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
assetManager =[AssetManager sharedInstance];
NSDictionary *videoListDict= [assetManager getAllVideoFromPlist];
NSArray *videoListKeyArray=[videoListDict allKeys];
if(videoListKeyArray.count!=0)
{
for(NSString *key in videoListKeyArray){
NSData *videoData = [videoListDict objectForKey:key];
Video *vidObject = (Video *)[NSKeyedUnarchiver unarchiveObjectWithData:videoData];
amazonManger=[AmazonManager sharedInstance];
[amazonManger uploadVideoWithVideoName:vidObject.videoName IsImage:NO VideoObject:vidObject];
[amazonManger uploadVideoWithVideoName:vidObject.thumbImageName IsImage:YES VideoObject:vidObject];
}
}
dispatch_async(dispatch_get_main_queue(), ^(void) {
//Stop your activity indicator or anything else with the GUI
//Code here is run on the main thread
[self startThreadForUpload];
// WARNING! - Don't update user interfaces from a background thread.
});
});
}
Upvotes: 2
Views: 391
Reputation: 5892
Why don't you just read the plist again into a new mutable dictionary, then remove the objects for the keys you already processed and repeat the process.
You should refactor the actual uploading functionality into a new method so you can call that recursively until all videos have been uploaded. Then you can perform the original selector again after a delay, or using dispatch_after.
Refactor the for loop out into a new method called uploadVideos:
and call it like so:
- (void)startThreadForUpload
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
assetManager = [AssetManager sharedInstance];
NSDictionary *videoListDict = [assetManager allVideoFromPlist];
// call the new method defined below to upload all videos from plist
[self uploadVideos:videoListDict.allValues];
// create a mutable dictionary and remove the videos that have already been uploaded
NSMutableDictionary *newVideoListDict = [assetManager getAllVideoFromPlist].mutableCopy;
[newVideoListDict removeObjectsForKeys:videoListDict.allKeys];
if (newVideoListDict.count > 0) {
// new videos, so upload them immediately
[self uploadVideos:newVideoListDict.allValues];
}
// start another upload after 300 seconds (5 minutes)
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(300 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self startThreadForUpload];
});
});
}
- (void)uploadVideos:(NSArray *)videos
{
AmazonManager *amazonManger = [AmazonManager sharedInstance];
for (NSData *videoData in videos) {
Video *vidObject = (Video *)[NSKeyedUnarchiver unarchiveObjectWithData:videoData];
// call a method defined in a category, described below
[amazonManager uploadVideo:vidObject];
}
}
You should define a category on AmazonManager to keep good separation of concerns:
// put this in AmazonManager+VideoUpload.h
@interface AmazonManager (VideoUpload)
- (void)uploadVideo:(Video *)video;
@end
// put this in AmazonManager+VideoUpload.m
@implementation AmazonManager (VideoUpload)
- (void)uploadVideo:(Video *)video
{
[self uploadVideoWithVideoName:video.videoName IsImage:NO VideoObject:video];
[self uploadVideoWithVideoName:video.thumbImageName IsImage:YES VideoObject:video];
}
@end
The problem now is that each time the method startThreadForUpload
is called, it will upload all the videos from your plist file. If this is how you always intend to read the videos that need to be uploaded, you should save which videos have already been uploaded, to avoid uploading them twice.
Hope that helps :)
Upvotes: 1