The data structure that captures the returned value – PHFetchResult

@interface PHFetchResult<__covariant ObjectType> : NSObject <NSCopying, NSFastEnumeration>
Copy the code

When class methods are used to retrieve objects on the PHAsset, PHCollection, PHAssetCollection, and PHCollectionList classes, Photos provides the result object in the fetch result.

You can access the contents of the fetch result using the methods and conventions used by the NSArray class. However, unlike the NSArray object, the PHFetchResult object loads content dynamically from the Photos library as needed, providing the best performance even when processing a large number of results.

Getting results provides thread-safe access to their content. After a fetch, the count value of the fetch result is constant, and all objects in the fetch result retain the same localIdentifier value.

In fact, PHFetchResul is an Array-like data structure specifically provided for PHPhotoKit, and most of its APIS are similar to NSArray.

@interface PHFetchResult<__covariant ObjectType> : NSObject <NSCopying, NSFastEnumeration>

@property (readonly) NSUInteger count;
- (ObjectType)objectAtIndex:(NSUInteger)index;
- (ObjectType)objectAtIndexedSubscript:(NSUInteger)idx;

- (BOOL)containsObject:(ObjectType)anObject;

- (NSUInteger)indexOfObject:(ObjectType)anObject;
- (NSUInteger)indexOfObject:(ObjectType)anObject inRange:(NSRange)range;

@property (nonatomic, readonly, nullable) ObjectType firstObject;
@property (nonatomic, readonly, nullable) ObjectType lastObject;

- (NSArray<ObjectType> *)objectsAtIndexes:(NSIndexSet *)indexes;

- (void)enumerateObjectsUsingBlock:(void (^)(ObjectType obj, NSUInteger idx, BOOL *stop))block;
- (void)enumerateObjectsWithOptions:(NSEnumerationOptions)opts usingBlock:(void (^)(ObjectType obj, NSUInteger idx, BOOL *stop))block;
- (void)enumerateObjectsAtIndexes:(NSIndexSet *)s options:(NSEnumerationOptions)opts usingBlock:(void (^)(ObjectType obj, NSUInteger idx, BOOL *stop))block;

- (NSUInteger)countOfAssetsWithMediaType:(PHAssetMediaType)mediaType;

@end
Copy the code