Frage PHChange changeDetailsForFetchResult - gibt immer nil zurück


Ich benutze die PHPhotoLibrary So rufen Sie mehrere Fotosammlungen ab:

_smartAlbumsFetchResult = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeAlbumMyPhotoStream options:nil];
[_smartAlbumsFetchResult enumerateObjectsUsingBlock:^(PHAssetCollection *collection, NSUInteger idx, BOOL *stop) {
    [self analyzeCollectionInternal:collection];
}];

_userAlbumsFetchResult = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAny options:userAlbumsOptions];
[_userAlbumsFetchResult enumerateObjectsUsingBlock:^(PHAssetCollection *collection, NSUInteger idx, BOOL *stop) {
    [self analyzeCollectionInternal:collection];
}];


Die Methode analyzeCollectionInternal speichert die PHAssetCollections zur späteren Verwendung und zeigt den Inhalt der Sammlungen an.

- (void)analyzeCollectionInternal:(PHAssetCollection *)collection {
    NSLog(@"Album Title %@", collection.localizedTitle);
    if (![_collections containsObject:collection]) {
        [_collections addObject:collection];
    }

    [...]
}


Zusätzlich habe ich einen Change Observer für die Photos-Bibliothek registriert:

[[PHPhotoLibrary sharedPhotoLibrary] registerChangeObserver:self];


Die Klasse, die als Beobachter fungiert, implementiert die PHPhotoLibraryChangeObserver Protokoll wie folgt:

- (void)photoLibraryDidChange:(PHChange *)changeInstance {
    dispatch_async(dispatch_get_main_queue(), ^{
        NSLog(@"PicturesDataSource - photoLibraryDidChange");
        NSMutableArray *collectionsToAnalyze = [NSMutableArray new];
        NSMutableArray *collectionsToDelete = [NSMutableArray new];

        if (_smartAlbumsFetchResult) {
            PHFetchResultChangeDetails *smartAlbumChanges = [changeInstance changeDetailsForFetchResult:_smartAlbumsFetchResult];
            NSLog(@"PictureDataSource - changeDetailsForFetchResult(_smartAlbumsFetchResult): %@", smartAlbumChanges);
            if (smartAlbumChanges) {
                _smartAlbumsFetchResult = [smartAlbumChanges fetchResultAfterChanges];
                [collectionsToAnalyze addObjectsFromArray:smartAlbumChanges.insertedObjects];
                [collectionsToAnalyze addObjectsFromArray:smartAlbumChanges.changedObjects];
                [collectionsToDelete addObjectsFromArray:smartAlbumChanges.removedObjects];
            }
        }

        if (_userAlbumsFetchResult) {
            PHFetchResultChangeDetails *userAlbumChanges = [changeInstance changeDetailsForFetchResult:_userAlbumsFetchResult];
            NSLog(@"PictureDataSource - changeDetailsForFetchResult(_userAlbumsFetchResult): %@", userAlbumChanges);
            if (userAlbumChanges) {
                _userAlbumsFetchResult = [userAlbumChanges fetchResultAfterChanges];
                [collectionsToAnalyze addObjectsFromArray:userAlbumChanges.insertedObjects];
                [collectionsToAnalyze addObjectsFromArray:userAlbumChanges.changedObjects];
                [collectionsToDelete addObjectsFromArray:userAlbumChanges.removedObjects];
            }
        }

        for (PHAssetCollection *collectionToAnalyze in collectionsToAnalyze) {
            [self analyzeCollection:collectionToAnalyze];
        }

        for (PHAssetCollection *collectionToDelete in collectionsToDelete) {
            [self deleteCollection:collectionToDelete];
        }
    });
}


Wenn ich jetzt die Fotos-App öffne, um Fotos vom "Alle Fotos" -SmartAlbum hinzuzufügen / zu ändern / zu löschen, wird der Change-Observer aufgerufen, aber der Anruf von

[changeInstance changeDetailsForFetchResult:_smartAlbumsFetchResult]

kommt nur zurück nil, auch wenn das PHChange-Objekt enthält insertedObjectIDs und changedObjectIDs. Gleiches gilt für Screenshots mit dem Home- und Lock-Button des iDevice.


6
2017-10-05 12:33


Ursprung


Antworten:


@property (nonatomic,strong) NSMutableArray *assetCollectionsArray;
@property (nonatomic,strong) NSMutableArray *phAssetArray;

-(void)initPHAssetData
{self.assetCollectionsArray = [self updateAssetCollections];
self.phAssetArray = [[NSMutableArray alloc] init];
for (int i=0; i<[self.assetCollectionsArray count]; i++) {
    PHFetchResult *cFetchResult = [self updateFetchRequest:(int)i];

    [self.phAssetArray addObject:cFetchResult];
}
}

- (NSMutableArray *)updateAssetCollections
{
NSArray *assetCollectionSubtypes = [NSArray arrayWithArray:self.phAssetSubTypeArray];

NSMutableDictionary *smartAlbums = [NSMutableDictionary dictionaryWithCapacity:assetCollectionSubtypes.count];
NSMutableArray *userAlbums = [NSMutableArray array];

PHFetchResult *smartAlbumsFetchResult = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeAny options:nil];

PHFetchResult *userAlbums1 = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAny options:nil];
NSArray *fetchResults = @[smartAlbumsFetchResult, userAlbums1];


for (PHFetchResult *fetchResult in fetchResults) {
    [fetchResult enumerateObjectsUsingBlock:^(PHAssetCollection *assetCollection, NSUInteger index, BOOL *stop) {
        PHAssetCollectionSubtype subtype = assetCollection.assetCollectionSubtype;

        if (subtype == PHAssetCollectionSubtypeAlbumRegular) {
            if ([assetCollectionSubtypes containsObject:@(subtype)])
                [userAlbums addObject:assetCollection];
        } else if ([assetCollectionSubtypes containsObject:@(subtype)]) {
            if (!smartAlbums[@(subtype)]) {
                smartAlbums[@(subtype)] = [NSMutableArray array];
            }
            [smartAlbums[@(subtype)] addObject:assetCollection];
        }
    }];
}

NSMutableArray *assetCollections = [NSMutableArray array];

// Fetch smart albums
for (NSNumber *assetCollectionSubtype in assetCollectionSubtypes) {
    NSArray *collections = smartAlbums[assetCollectionSubtype];

    if (collections) {
        [assetCollections addObjectsFromArray:collections];
    }
}

// Fetch user albums
[userAlbums enumerateObjectsUsingBlock:^(PHAssetCollection *assetCollection, NSUInteger index, BOOL *stop) {
    [assetCollections addObject:assetCollection];
}];

return assetCollections;
}

- (PHFetchResult *)updateFetchRequest:(int)index
{
PHFetchResult *phFetchResult;
if (self.assetCollectionsArray || [self.assetCollectionsArray count]>0) {
    PHFetchOptions *options = [PHFetchOptions new];

    PHAssetCollection *assetCollection = [self.assetCollectionsArray objectAtIndex:index];

    switch (assetCollection.assetCollectionSubtype) {
        case PHAssetCollectionSubtypeAlbumMyPhotoStream:
            options.predicate = [NSPredicate predicateWithFormat:@"mediaType == %ld", PHAssetMediaTypeImage];
            break;
        case PHAssetCollectionSubtypeSmartAlbumVideos:
            options.predicate = [NSPredicate predicateWithFormat:@"mediaType == %ld", PHAssetMediaTypeVideo];
            break;
        case PHAssetCollectionSubtypeSmartAlbumUserLibrary:
            options.predicate = [NSPredicate predicateWithFormat:@"mediaType == %ld", PHAssetMediaTypeImage];
            break;
        default:
            options.predicate = [NSPredicate predicateWithFormat:@"mediaType == %ld || mediaType == %ld", PHAssetMediaTypeVideo,PHAssetMediaTypeImage];
            break;
            break;
    }

    phFetchResult = [PHAsset fetchAssetsInAssetCollection:[self.assetCollectionsArray objectAtIndex:index] options:options];
} else {
    phFetchResult = nil;
}

return phFetchResult;
}

dann: PHFetchResultChangeDetails * smartAlbumChanges = [Änderungsinstanz changeDetailsForFetchResult: [self.phAssetArray objectAtIndex: 0]];


0
2017-07-21 09:04