File changes in the Documents folder

Source: Internet
Author: User

// 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.