2

I'm using PHPhotoLibrary to save and fetch images for my application's album in iPhone.

When my album has a lot of images (about 5,000 still images), my application download 10 images from network, then save to Camera roll and add to my application's album.

At the same time, the application observe photoLibraryDidChange callback to know the added images, but it notify only 5 inserted images.

I finish my application by pushing HOME button and check Camera roll and my album in Photo App. There are correctly 5010 images.

Maybe photoLibraryDidChagne doesn't notify all changes ?

My code is like below.

- (void)photoLibraryDidChange:(PHChange *)changeInstance
{
    // dispatch main queue
    dispatch_async(dispatch_get_main_queue(), ^{
        [self handleChangedLibrary:changeInstance];
    });
}

- (void)handleChangedLibrary:(PHChange *)changeInstance
{
    PHFetchResultChangeDetails *fetchResultChangeDetails = [changeInstance changeDetailsForFetchResult:_assetsFetchResult];

    if (!fetchResultChangeDetails) {
        NSLog(@"### No change in fetchResultChangeDetails ###");
        return;
    }

    if (![fetchResultChangeDetails hasIncrementalChanges]) {
        [self fetchAllAssets];
        return;
    }

    NSArray *insertedObjects = [fetchResultChangeDetails insertedObjects];
    if (insertedObjects) {
        for (PHAsset *asset in insertedObjects) {
            if (asset.mediaType == PHAssetMediaTypeImage) {
                NSLog(@"asset=%@", asset);
                [_stillImageAssetArray addObject:asset];
            }
        }
    }

    self.assetsFetchResult = [PHAsset fetchAssetsInAssetCollection:_assetCollection options:nil];
}

I checked inserted asset by NSLog and Debugger and it truly updated 5 images.

There is no notification for the other 5 images.

4

1 回答 1

0

我修复了代码中的错误点,并确认修改后问题不会发生。

错误点:

  1. 在某些情况下,我的代码'return;' 在回调中获取资产集合之前。
  2. 我应该从fetchResultChangeDetails.fetchResultAfterChangesinstaed 中获取新结果[PHAsset fetchAssetsInAssetCollection:options:]

我参考了以下 Apple 的代码进行修改。

PHPhotoLibraryChangeObserver https://developer.apple.com/library/ios/documentation/Photos/Reference/PHPhotoLibraryChangeObserver_Protocol/

我的固定代码如下所示。谢谢你。

- (void)handleChangedLibrary:(PHChange *)changeInstance
{
    // Check for changes to the list of assets (insertions, deletions, moves, or updates).
    PHFetchResultChangeDetails *fetchResultChangeDetails = [changeInstance changeDetailsForFetchResult:_assetsFetchResult];
    if (!fetchResultChangeDetails) {
        NSLog(@"### No change in fetchResultChangeDetails ###");
        return;
    }

    // Get the new fetch result for future change tracking.
    self.assetsFetchResult = fetchResultChangeDetails.fetchResultAfterChanges;

    if (![fetchResultChangeDetails hasIncrementalChanges]) {
        [self fetchAllAssets];        
        return;
    }

    NSArray *insertedObjects = [fetchResultChangeDetails insertedObjects];
    if (insertedObjects) {
        for (PHAsset *asset in insertedObjects) {
            if (asset.mediaType == PHAssetMediaTypeImage) {
                NSLog(@"asset=%@", asset);
                [_stillImageAssetArray addObject:asset];
            }
        }
    }
}
于 2016-08-10T07:51:15.267 回答