iTunes File Sharing app: realtime monitoring for incoming datas

前端 未结 2 1574
[愿得一人]
[愿得一人] 2020-12-09 06:56

I\'m working on iOS project that supports iTunes file sharing feature. The goal is realtime tracking incoming/changed data\'s.

I\'m

相关标签:
2条回答
  • 2020-12-09 07:09

    I found two rather reliable (i.e. not 100% reliable but reliable enough for my needs) approaches, which only work in conjunction with polling the contents of the directory:

    1. Check NSURLContentModificationDateKey. While the file is being transferred, this value is set to the current date. After transfer has finished, it is set to the value the original file had: BOOL busy = (-1.0 * [modDate timeintervalSinceNow]) < pollInterval;
    2. Check NSURLThumbnailDictionaryKey. While the file is being transferred, this value is nil, afterwards it cointains a thumbnail, but probably only for file types from which the system can produce a thumbnail. Not a problem for me cause I only care about images and videos, but maybe for you. While this is more reliable than solution 1, it hammers the CPU quite a bit and may even cause your app to get killed if you have a lot of files in the import directory.

    Dispatch sources and polling can be combined, i.e. when a dispatch source detects a change, start polling until no busy files are left.

    0 讨论(0)
  • 2020-12-09 07:30

    You can use GCD's dispatch sources mechanism - using it you can observe particular system events (in your case, this is vnode type events, since you're working with file system). To setup observer for particular directory, i used code like this:

    - (dispatch_source_t) fileSystemDispatchSourceAtPath:(NSString*) path
    {
        int fileDescr = open([path fileSystemRepresentation], O_EVTONLY);// observe file system events for particular path - you can pass here Documents directory path
        //observer queue is my private dispatch_queue_t object
        dispatch_source_t source = dispatch_source_create(DISPATCH_SOURCE_TYPE_VNODE, fileDescr, DISPATCH_VNODE_ATTRIB| DISPATCH_VNODE_WRITE|DISPATCH_VNODE_LINK|DISPATCH_VNODE_EXTEND, observerQueue);// create dispatch_source object to observe vnode events
        dispatch_source_set_registration_handler(source, ^{
            NSLog(@"registered for observation");
            //event handler is called each time file system event of selected type (DISPATCH_VNODE_*) has occurred
            dispatch_source_set_event_handler(source, ^{
    
                dispatch_source_vnode_flags_t flags = dispatch_source_get_data(source);//obtain flags
                NSLog(@"%lu",flags);
    
                if(flags & DISPATCH_VNODE_WRITE)//flag is set to DISPATCH_VNODE_WRITE every time data is appended to file
                {
                    NSLog(@"DISPATCH_VNODE_WRITE");
                    NSDictionary* dict = [[NSFileManager defaultManager] attributesOfItemAtPath:path error:nil];
                    float size = [[dict valueForKey:NSFileSize] floatValue];
                    NSLog(@"%f",size);
                }
                if(flags & DISPATCH_VNODE_ATTRIB)//this flag is passed when file is completely written.
                {
                    NSLog(@"DISPATCH_VNODE_ATTRIB");
                    dispatch_source_cancel(source);
                }
                if(flags & DISPATCH_VNODE_LINK)
                {
                    NSLog(@"DISPATCH_VNODE_LINK");
                }
                if(flags & DISPATCH_VNODE_EXTEND)
                {
                    NSLog(@"DISPATCH_VNODE_EXTEND");
                }
                NSLog(@"file = %@",path);
                NSLog(@"\n\n");
            });
    
            dispatch_source_set_cancel_handler(source, ^{
                close(fileDescr);
            });
        });
    
        //we have to resume dispatch_objects
        dispatch_resume(source);
    
        return source;
    }
    
    0 讨论(0)
提交回复
热议问题