Commit 5c14de2d by Daniel Dahan

issue-479: added helper methods to simplify fetch requests in PhotoLibrary

parent 2f4ac3e2
...@@ -437,26 +437,42 @@ extension PhotoLibrary { ...@@ -437,26 +437,42 @@ extension PhotoLibrary {
/// PHCollection. /// PHCollection.
extension PhotoLibrary { extension PhotoLibrary {
/** /**
Fetch PHCollection based on a type and subtype. Fetches PHCollections in a given PHCollectionList.
- Parameter in collectionList: A PHCollectionList.
- Parameter options: An optional PHFetchOptions object.
- Parameter completion: A completion callback.
*/
public func fetchCollections(in collectionList: PHCollectionList, options: PHFetchOptions?, completion: ([PHCollection], PHFetchResult<PHCollection>) -> Void) {
fetchCollections(fetchResult: PHCollection.fetchCollections(in: collectionList, options: options), completion: completion)
}
/**
Fetches PHCollections based on a type and subtype.
- Parameter with type: A PHCollectionListType. - Parameter with type: A PHCollectionListType.
- Parameter subtype: A PHCollectionListSubtype. - Parameter subtype: A PHCollectionListSubtype.
- Parameter options: An optional PHFetchOptions object. - Parameter options: An optional PHFetchOptions object.
- Parameter completion: A completion callback. - Parameter completion: A completion callback.
*/ */
public func fetchTopLevelUserCollections(with options: PHFetchOptions?, completion: ([PHCollection], PHFetchResult<PHCollection>) -> Void) { public func fetchTopLevelUserCollections(with options: PHFetchOptions?, completion: ([PHCollection], PHFetchResult<PHCollection>) -> Void) {
fetchCollections(fetchResult: PHCollection.fetchTopLevelUserCollections(with: nil), completion: completion)
}
/**
Fetches the PHCollections for a given PHFetchResult<PHCollection>.
- Parameter fetchResult: A PHFetchResult<PHCollection>.
- Parameter completion: A completion block.
*/
private func fetchCollections(fetchResult: PHFetchResult<PHCollection>, completion: ([PHCollection], PHFetchResult<PHCollection>) -> Void) {
var collections = [PHCollection]() var collections = [PHCollection]()
let fetchResult = PHCollection.fetchTopLevelUserCollections(with: nil)
defer { fetchResult.enumerateObjects({ (collection, _, _) in
collections.append(collection)
})
DispatchQueue.main.async { [collections = collections, fetchResult = fetchResult, completion = completion] in DispatchQueue.main.async { [collections = collections, fetchResult = fetchResult, completion = completion] in
completion(collections, fetchResult) completion(collections, fetchResult)
} }
} }
fetchResult.enumerateObjects(options: [.concurrent]) { (collection, _, _) in
collections.append(collection)
}
}
} }
/// PHCollectionList. /// PHCollectionList.
...@@ -471,18 +487,7 @@ extension PhotoLibrary { ...@@ -471,18 +487,7 @@ extension PhotoLibrary {
- Parameter completion: A completion callback. - Parameter completion: A completion callback.
*/ */
public func fetchCollectionListsContaining(_ collection: PHCollection, options: PHFetchOptions?, completion: ([PHCollectionList], PHFetchResult<PHCollectionList>) -> Void) { public func fetchCollectionListsContaining(_ collection: PHCollection, options: PHFetchOptions?, completion: ([PHCollectionList], PHFetchResult<PHCollectionList>) -> Void) {
var lists = [PHCollectionList]() fetchCollectionLists(fetchResult: PHCollectionList.fetchCollectionListsContaining(collection, options: options), completion: completion)
let fetchResult = PHCollectionList.fetchCollectionListsContaining(collection, options: options)
defer {
DispatchQueue.main.async { [lists = lists, fetchResult = fetchResult, completion = completion] in
completion(lists, fetchResult)
}
}
fetchResult.enumerateObjects({ (list, _, _) in
lists.append(list)
})
} }
/** /**
...@@ -493,18 +498,7 @@ extension PhotoLibrary { ...@@ -493,18 +498,7 @@ extension PhotoLibrary {
- Parameter completion: A completion callback. - Parameter completion: A completion callback.
*/ */
public func fetchCollectionList(with type: PHCollectionListType, subtype: PHCollectionListSubtype, options: PHFetchOptions?, completion: ([PHCollectionList], PHFetchResult<PHCollectionList>) -> Void) { public func fetchCollectionList(with type: PHCollectionListType, subtype: PHCollectionListSubtype, options: PHFetchOptions?, completion: ([PHCollectionList], PHFetchResult<PHCollectionList>) -> Void) {
var lists = [PHCollectionList]() fetchCollectionLists(fetchResult: PHCollectionList.fetchCollectionLists(with: type, subtype: subtype, options: options), completion: completion)
let fetchResult = PHCollectionList.fetchCollectionLists(with: type, subtype: subtype, options: options)
defer {
DispatchQueue.main.async { [lists = lists, fetchResult = fetchResult, completion = completion] in
completion(lists, fetchResult)
}
}
fetchResult.enumerateObjects({ (list, _, _) in
lists.append(list)
})
} }
/** /**
...@@ -517,18 +511,7 @@ extension PhotoLibrary { ...@@ -517,18 +511,7 @@ extension PhotoLibrary {
- Parameter completion: A completion callback. - Parameter completion: A completion callback.
*/ */
public func fetchCollectionLists(withLocalIdentifiers identifiers: [String], options: PHFetchOptions?, completion: ([PHCollectionList], PHFetchResult<PHCollectionList>) -> Void) { public func fetchCollectionLists(withLocalIdentifiers identifiers: [String], options: PHFetchOptions?, completion: ([PHCollectionList], PHFetchResult<PHCollectionList>) -> Void) {
var lists = [PHCollectionList]() fetchCollectionLists(fetchResult: PHCollectionList.fetchCollectionLists(withLocalIdentifiers: identifiers, options: options), completion: completion)
let fetchResult = PHCollectionList.fetchCollectionLists(withLocalIdentifiers: identifiers, options: options)
defer {
DispatchQueue.main.async { [lists = lists, fetchResult = fetchResult, completion = completion] in
completion(lists, fetchResult)
}
}
fetchResult.enumerateObjects({ (list, _, _) in
lists.append(list)
})
} }
/** /**
...@@ -540,19 +523,8 @@ extension PhotoLibrary { ...@@ -540,19 +523,8 @@ extension PhotoLibrary {
- Parameter options: An optional PHFetchOptions object. - Parameter options: An optional PHFetchOptions object.
- Parameter completion: A completion callback. - Parameter completion: A completion callback.
*/ */
public class func fetchCollectionLists(with collectionListType: PHCollectionListType, subtype: PHCollectionListSubtype, options: PHFetchOptions?, completion: ([PHCollectionList], PHFetchResult<PHCollectionList>) -> Void) { public func fetchCollectionLists(with collectionListType: PHCollectionListType, subtype: PHCollectionListSubtype, options: PHFetchOptions?, completion: ([PHCollectionList], PHFetchResult<PHCollectionList>) -> Void) {
var lists = [PHCollectionList]() fetchCollectionLists(fetchResult: PHCollectionList.fetchCollectionLists(with: collectionListType, subtype: subtype, options: options), completion: completion)
let fetchResult = PHCollectionList.fetchCollectionLists(with: collectionListType, subtype: subtype, options: options)
defer {
DispatchQueue.main.async { [lists = lists, fetchResult = fetchResult, completion = completion] in
completion(lists, fetchResult)
}
}
fetchResult.enumerateObjects({ (list, _, _) in
lists.append(list)
})
} }
/** /**
...@@ -562,19 +534,8 @@ extension PhotoLibrary { ...@@ -562,19 +534,8 @@ extension PhotoLibrary {
- Parameter options: An optional PHFetchOptions object. - Parameter options: An optional PHFetchOptions object.
- Parameter completion: A completion callback. - Parameter completion: A completion callback.
*/ */
public class func fetchMomentLists(with momentListSubtype: PHCollectionListSubtype, containingMoment moment: PHAssetCollection, options: PHFetchOptions?, completion: ([PHCollectionList], PHFetchResult<PHCollectionList>) -> Void) { public func fetchMomentLists(with momentListSubtype: PHCollectionListSubtype, containingMoment moment: PHAssetCollection, options: PHFetchOptions?, completion: ([PHCollectionList], PHFetchResult<PHCollectionList>) -> Void) {
var lists = [PHCollectionList]() fetchCollectionLists(fetchResult: PHCollectionList.fetchMomentLists(with: momentListSubtype, containingMoment: moment, options: options), completion: completion)
let fetchResult = PHCollectionList.fetchMomentLists(with: momentListSubtype, containingMoment: moment, options: options)
defer {
DispatchQueue.main.async { [lists = lists, fetchResult = fetchResult, completion = completion] in
completion(lists, fetchResult)
}
}
fetchResult.enumerateObjects({ (list, _, _) in
lists.append(list)
})
} }
/** /**
...@@ -584,18 +545,24 @@ extension PhotoLibrary { ...@@ -584,18 +545,24 @@ extension PhotoLibrary {
- Parameter completion: A completion callback. - Parameter completion: A completion callback.
*/ */
public func fetchMomentLists(with momentListSubtype: PHCollectionListSubtype, options: PHFetchOptions?, completion: ([PHCollectionList], PHFetchResult<PHCollectionList>) -> Void) { public func fetchMomentLists(with momentListSubtype: PHCollectionListSubtype, options: PHFetchOptions?, completion: ([PHCollectionList], PHFetchResult<PHCollectionList>) -> Void) {
var lists = [PHCollectionList]() fetchCollectionLists(fetchResult: PHCollectionList.fetchMomentLists(with: momentListSubtype, options: options), completion: completion)
let fetchResult = PHCollectionList.fetchMomentLists(with: momentListSubtype, options: options)
defer {
DispatchQueue.main.async { [lists = lists, fetchResult = fetchResult, completion = completion] in
completion(lists, fetchResult)
}
} }
/**
Fetches the PHCollectionLists for a given PHFetchResult<PHCollectionList>.
- Parameter fetchResult: A PHFetchResult<PHCollectionList>.
- Parameter completion: A completion block.
*/
private func fetchCollectionLists(fetchResult: PHFetchResult<PHCollectionList>, completion: ([PHCollectionList], PHFetchResult<PHCollectionList>) -> Void) {
var lists = [PHCollectionList]()
fetchResult.enumerateObjects({ (list, _, _) in fetchResult.enumerateObjects({ (list, _, _) in
lists.append(list) lists.append(list)
}) })
DispatchQueue.main.async { [lists = lists, fetchResult = fetchResult, completion = completion] in
completion(lists, fetchResult)
}
} }
} }
...@@ -608,22 +575,58 @@ extension PhotoLibrary { ...@@ -608,22 +575,58 @@ extension PhotoLibrary {
- Parameter completion: A completion block. - Parameter completion: A completion block.
*/ */
public func fetchAssets(in assetCollection: PHAssetCollection, options: PHFetchOptions?, completion: ([PHAsset], PHFetchResult<PHAsset>) -> Void) { public func fetchAssets(in assetCollection: PHAssetCollection, options: PHFetchOptions?, completion: ([PHAsset], PHFetchResult<PHAsset>) -> Void) {
var fetchResult: PHFetchResult<PHAsset>! fetchAssets(fetchResult: PHAsset.fetchAssets(in: assetCollection, options: options), completion: completion)
var assets = [PHAsset]() }
defer { /**
DispatchQueue.main.async { [assets = assets, fetchResult = fetchResult, completion = completion] in Fetch the PHAssets with a given Array of identifiers.
completion(assets, fetchResult!) - Parameter withLocalIdentifiers identifiers: A Array of
String identifiers.
- Parameter options: An optional PHFetchOptions object.
- Parameter completion: A completion block.
*/
public func fetchAssets(withLocalIdentifiers identifiers: [String], options: PHFetchOptions?, completion: ([PHAsset], PHFetchResult<PHAsset>) -> Void) {
fetchAssets(fetchResult: PHAsset.fetchAssets(withLocalIdentifiers: identifiers, options: options), completion: completion)
}
/**
Fetch key assets.
- Parameter in assetCollection: A PHAssetCollection.
- Parameter options: An optional PHFetchOptions object.
- Parameter completion: A completion block.
- Returns: An optional PHFetchResult<PHAsset> object.
*/
public func fetchKeyAssets(in assetCollection: PHAssetCollection, options: PHFetchOptions?, completion: ([PHAsset], PHFetchResult<PHAsset>) -> Void) -> PHFetchResult<PHAsset>? {
guard let fetchResult = PHAsset.fetchKeyAssets(in: assetCollection, options: options) else {
return nil
} }
fetchAssets(fetchResult: fetchResult, completion: completion)
return fetchResult
} }
fetchResult = PHAsset.fetchAssets(in: assetCollection, options: options) /**
Fetch a burst asset with a given burst identifier.
- Parameter withBurstIdentifier burstIdentifier: A
PHAssetCollection.
- Parameter options: An optional PHFetchOptions object.
- Parameter completion: A completion block.
*/
public func fetchAssets(withBurstIdentifier burstIdentifier: String, options: PHFetchOptions?, completion: ([PHAsset], PHFetchResult<PHAsset>) -> Void) {
fetchAssets(fetchResult: PHAsset.fetchAssets(withBurstIdentifier: burstIdentifier, options: options), completion: completion)
}
fetchResult.enumerateObjects({ (asset, _, _) in
assets.append(asset) /**
}) Fetches PHAssetSourceTypeUserLibrary assets by default (use
includeAssetSourceTypes option to override).
- Parameter with options: An optional PHFetchOptions object.
- Parameter completion: A completion block.
*/
public func fetchAssets(with options: PHFetchOptions?, completion: ([PHAsset], PHFetchResult<PHAsset>) -> Void) {
fetchAssets(fetchResult: PHAsset.fetchAssets(with: options), completion: completion)
} }
/** /**
Fetch the PHAssets with a given media type. Fetch the PHAssets with a given media type.
- Parameter in mediaType: A PHAssetMediaType. - Parameter in mediaType: A PHAssetMediaType.
...@@ -631,20 +634,35 @@ extension PhotoLibrary { ...@@ -631,20 +634,35 @@ extension PhotoLibrary {
- Parameter completion: A completion block. - Parameter completion: A completion block.
*/ */
public func fetchAssets(with mediaType: PHAssetMediaType, options: PHFetchOptions?, completion: ([PHAsset], PHFetchResult<PHAsset>) -> Void) { public func fetchAssets(with mediaType: PHAssetMediaType, options: PHFetchOptions?, completion: ([PHAsset], PHFetchResult<PHAsset>) -> Void) {
var fetchResult: PHFetchResult<PHAsset>! fetchAssets(fetchResult: PHAsset.fetchAssets(with: mediaType, options: options), completion: completion)
var assets = [PHAsset]()
defer {
DispatchQueue.main.async { [assets = assets, fetchResult = fetchResult, completion = completion] in
completion(assets, fetchResult!)
} }
/**
AssetURLs are URLs retrieved from ALAsset's
ALAssetPropertyAssetURL.
- Parameter withALAssetURLs assetURLs: A PHAssetMediaType.
- Parameter options: An optional PHFetchOptions object.
- Parameter completion: A completion block.
*/
public func fetchAssets(withALAssetURLs assetURLs: [URL], options: PHFetchOptions?, completion: ([PHAsset], PHFetchResult<PHAsset>) -> Void) {
fetchAssets(fetchResult: PHAsset.fetchAssets(withALAssetURLs: assetURLs, options: options), completion: completion)
} }
fetchResult = PHAsset.fetchAssets(with: mediaType, options: options) /**
Fetches the PHAssets for a given PHFetchResult<PHAsset>.
- Parameter fetchResult: A PHFetchResult<PHAsset>.
- Parameter completion: A completion block.
*/
private func fetchAssets(fetchResult: PHFetchResult<PHAsset>, completion: ([PHAsset], PHFetchResult<PHAsset>) -> Void) {
var assets = [PHAsset]()
fetchResult.enumerateObjects({ (asset, _, _) in fetchResult.enumerateObjects({ (asset, _, _) in
assets.append(asset) assets.append(asset)
}) })
DispatchQueue.main.async { [assets = assets, fetchResult = fetchResult, completion = completion] in
completion(assets, fetchResult)
}
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment