But at some point, we want the main thread to wait for the IO operation to complete--for example, after the main thread creates a folder, waits for completion, and then creates the file in the folder.
The NSObject class has a method that performSelectorOnMainThread:withObject:waitUntilDone the main thread to hang, but the selector can take at most one of the parameters that can be passed. You need to use the Nsinvocation class to solve this problem.
The code is as follows:
-(void) Createrecorddirectoryblockingmainthread
{
Nsfilemanager *DFM = [Nsfilemanager Defaultmanager];
Sel sel = @selector (createdirectoryatpath:
Withintermediatedirectories:
Attributes
Error:);
Nsmethodsignature *sig = [DFM Methodsignatureforselector:sel];
The signature of the method actually contains the target, selector, and type information for each parameter, but does not contain the memory address of the target and method. This information is to accurately get the values of each parameter when the function presses the stack
Nsinvocation *IVCT = [Nsinvocation Invocationwithmethodsignature:sig];
[Ivct SETTARGET:DFM];
[Ivct Setselector:sel]; Provide the memory address of the method
Filepathmanager *fpm = [Filepathmanager sharedinstance];
NSString *path = Fpm.programrecorddirectorypath;
[Ivct Setargument:&path Atindex:2]; The index of the parameter in the array starts at 2
[Ivct setargument: (void *) &bool_no atindex:3]; Type conversions of (void *) can avoid compiler warnings
[Ivct setargument: (void *) &id_nil atindex:4];
[Ivct setargument: (void *) &void_null atindex:5];
[Ivct Performselectoronmainthread: @selector (Invoke)
Withobject:nil
Waituntildone:yes]; Suspend main thread
}