How to show migration progress of NSMigrationManager in a UILabel?

隐身守侯 提交于 2019-12-04 20:12:08

You precisely need to perform the migration on a background thread, and update the UI as you get progress notifications on the main thread.

- (void)startMigration {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
    NSMigrationManager  * manager = [[NSMigrationManager alloc] initWithSourceModel:sourceModel destinationModel:destinationModel];
    [manager addObserver:self forKeyPath:@"migrationProgress" options:0 context:NULL];
    BOOL success = [manager migrateStoreFromURL:sourceStoreURL type:type options:nil withMappingModel:mappingModel toDestinationURL:destinationStoreURL destinationType:type destinationOptions:nil error:&error];
    [manager removeObserver:self forKeyPath:@"migrationProgress"];
    if(success) {
        dispatch_async(dispatch_get_main_queue(), ^{
        // Go back to the main thread and continue…
        });
    }
}

And when getting notifications:

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    dispatch_sync(dispatch_get_main_queue(), ^{
        CGFloat progress = [(NSMigrationManager *)object migrationProgress];
        // Update UI progress indicator
    });
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!