KVO enables display of custom file copy progress and kvo custom progress

Source: Internet
Author: User

KVO enables display of custom file copy progress and kvo custom progress

1. Create a file

  Description: Custom file class. File Creation and copy are implemented through NSFileManager and NSFileHandle. To control the concurrent memory usage, you can control the fixed length of each assignment to replicate multiple times:

NSString * path = NSHomeDirectory (); path = [path stringByAppendingPathComponent: @ "deskTop/Boby. m "]; NSString * target = NSHomeDirectory (); target = [target stringByAppendingPathComponent: @" deskTop/target. m "]; NSFileManager * manager = [NSFileManager defaultManager]; // check and create a file if (! [Manager fileExistsAtPath: path]) {[manager createFileAtPath: path contents: nil attributes: nil] ;}if (! [Manager fileExistsAtPath: target]) {[manager createFileAtPath: target contents: nil attributes: nil];} NSDictionary * dic = [manager attributesOfItemAtPath: path error: nil]; NSFileHandle * handle = [NSFileHandle fileHandleForReadingAtPath: path]; NSFileHandle * handletTarget = [NSFileHandle handle: target]; int total = (int) [dic [@ "NSFileSize"] integerValue]; self. totalSize = total; int per = 50; Int count = total % per = 0? Total/per: total/per + 1; for (int I = 0; I <count; I ++) {[handle seekToFileOffset: self. nowSize]; NSData * data = [handle readDataOfLength: per]; int tem = per * (I + 1); if (tem> total) {tem = total;} self. nowSize = tem; [handletTarget seekToEndOfFile]; [handletTarget writeData: data]; [NSThread sleepForTimeInterval: 0.2];} [handle closeFile]; [handletTarget closeFile];
2. Set the observer

  Description: Custom Users can dynamically observe the progress of the current file copy by setting the observer and display it to the console or output it to the UI. It also provides method interfaces to start file copy.

-(Id) initWithFile :( FileMake *) files {self = [super init]; if (self) {self. file = files; [self. file addObserver: self forKeyPath: @ "nowSize" options: NSKeyValueObservingOptionNew context: nil];} return self;}-(void) observeValueForKeyPath :( NSString *) keyPath ofObject :( id) object change :( NSDictionary *) change context :( void *) context {CGFloat all = self. file. totalSize; CGFloat now = [[change objectForKey: @ "new"] floatValue]; CGFloat result = now/all; NSLog (@ "%. 2f ", result); // you must not forget to destroy the current observer if (result = 1) {[self. file removeObserver: self forKeyPath: @ "nowSize"] ;}}- (void) begin {[self. file startCopy];}

 

Related Article

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.