Skip to content

Instantly share code, notes, and snippets.

@pita5
Created October 25, 2012 11:53
Show Gist options
  • Save pita5/3952176 to your computer and use it in GitHub Desktop.
Save pita5/3952176 to your computer and use it in GitHub Desktop.
dispatch_queue_t queue = dispatch_queue_create("somequeue", DISPATCH_QUEUE_CONCURRENT);
dispatch_apply(1000000, queue, ^(size_t i) {
NSUInteger length = [self saveSomethingToDisk];
NSUInteger gotLength = [self getSoemthingFromDisk];
if (length != gotLength)
{
NSLog(@"Something went wrong!");
}
});
- (NSUInteger)saveSomethingToDisk
{
NSString* cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
NSString* someFile = [cachesPath stringByAppendingPathComponent:@"somefile"];
NSData* data = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"apple_rainbow_logo" ofType:@"jpg"]];
BOOL ok = NO;
@try
{
ok = [NSKeyedArchiver archiveRootObject:data toFile:someFile];
}
@catch (NSException *exception)
{
NSLog(@"Got an exception archiving");
}
@finally
{
NSLog(@"written %@", ok?@"OK":@"FAILED");
return data.length;
}
}
- (NSUInteger)getSoemthingFromDisk
{
NSString* cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
NSString* someFile = [cachesPath stringByAppendingPathComponent:@"somefile"];
NSData* data = nil;
@try {
data = [NSKeyedUnarchiver unarchiveObjectWithFile:someFile];
}
@catch (NSException *exception) {
NSLog(@"Got an exception unarchiving");
}
@finally
{
NSLog(@"retreived %@",data?@"OK":@"FAILED");
return data?data.length:0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment