// When the documents in documents change, start the timer, calculate the size of each second, when the size does not change when the transfer is complete, start to refresh. @property (nonatomic, strong) Nstimer *timer; // file Size in original documents @property (nonatomic, assign) Nsinteger filessize; // Documents After the change in size @property (nonatomic, assign) Nsinteger foundsize;
-(Nstimer *) Timer { if (! _timer) { = [[Nstimer alloc] init]; [_timer setfiredate:[nsdate distantfuture]; } return _timer;}
- (void) viewdidload {[Super viewdidload]; Self.view.backgroundColor=[Uicolor Whitecolor]; _filemanager=[Nsfilemanager Defaultmanager]; Self.timer= [Nstimer scheduledtimerwithtimeinterval:1. F target:self Selector: @selector (comparesize) Userinfo:nil Repeats:yes]; [Self.timer setfiredate:[nsdate distantfuture]; //* * Monitor files in documents folder for changes int ConstFolderdescriptor =open ([Kdocumentpath filesystemrepresentation], o_evtonly); dispatch_source_t _directorysource=dispatch_source_create (Dispatch_source_type_vnode, Folderdescriptor, Dispatch_vnode_write, DISPATCH_TARGET_ Queue_default); Dispatch_source_set_event_handler (_directorysource,^{unsignedLong Constdata =Dispatch_source_get_data (_directorysource); if(Data &dispatch_vnode_write) { //Do all the work on the main thread,//including timer scheduling, notifications deliveringDispatch_async (Dispatch_get_main_queue (), ^{[Self directorydidchanger]; }); } }); Dispatch_source_set_cancel_handler (_directorysource,^{Close (folderdescriptor); }); Dispatch_resume (_directorysource);}
//When a file is sent in the Documengs folder- (void) Directorydidchanger {_filessize=[self getsizeoffilepath:kdocumentpath]; Dispatch_after (Dispatch_time (Dispatch_time_now, (int64_t) (1. f * nsec_per_sec)), Dispatch_get_main_queue (), ^{[Self.timer setfiredate:[nsdate Date]]; }); }- (void) Dealloc {//Remove Timer[Self.timer invalidate];}//Get all Files-(Nsarray *) directoryfiles {Nsarray*files =[_filemanager Subpathsatpath:kdocumentpath]; returnfiles;}//Compare file sizes to listen for files that are still being transferred- (void) comparesize {_foundsize=[self getsizeoffilepath:kdocumentpath]; if(_foundsize = = _filessize) {//Refresh the data if the size no longer changes[Self.timer setfiredate:[nsdate distantfuture]; [Self selectdate]; //Start refresh Data } _filessize=_foundsize;}
File changes in the Documents folder