I have 2 method calls exposed in my library as follows:
-(void) createFile {
dispatch_async(serialQueue, ^{
[fileObj createFile:fileInfo completion:^(void){
//completion block C1
}];
});
}
-(void) readFile:(NSUInteger)timeStamp {
dispatch_async(serialQueue, ^{
[fileObj readFile:timeStamp completion:^(void){
//completion block C2
}];
});
}
Now the createFile:fileInfo:completion
and readFile:timeStamp:completion
are in turn XPC calls that call into a process P1
. Their implementation inside the process P1 looks like this:
@interface FileObject : NSObject
+ (instancetype) sharedInstance;
@property (nonatomic, strong) NSData *fileContents;
@end
@implementation FileObject
+ (instancetype)sharedInstance
{
static FileObject *sharedObj = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedObj = [[self alloc] init];
});
return sharedObj;
}
- (void)createFile:(FileInfo *)fileInfo
completion:(void (^))completion {
FileObject *f = [FileObject sharedInstance];
//lock?
f.fileContents = //call some method;
//unlock
}
- (void)readFile:(NSUInteger)timeStamp
completion:(void (^))completion {
FileObject *f = [FileObject sharedInstance];
//do stuff with f.fileContents
}
@end
The point to be noted is that,a call to method createFile
is capable of making modifications to the singleton obj and readFile:fileInfo
is always called after createFile
(serial queue used at invocation).
My question is given the serial queue usage for the two methods exposed in my library,
- Do I still need to lock and unlock when I modify
f.fileContents
inreadFileInfo:FileInfo:completion
? - How about if multiple different processes call into my library? Will XPC to
P1
handle that automatically or should I have to do something about it ?