3

在 React Native CameraRoll 上调用 getPhotos 函数时,是否有一次获取多少张照片的最佳实践?

CameraRoll.getPhotos({
    first: 10000,
    assetType: 'Photos',
})

我想通过创建一个while循环来从用户设备上获取每张照片,直到我拥有每张照片。一次获取更多,即 10,000 以上,并且通过 while 循环进行更少的迭代,还是应该进行较小的调用以一次获取 100、1000 等照片?

ReactNative 文档:CameraRoll

我的完整代码块:

async fetchAllImages() {
    let morePhotosToFetch = true;
    let photosAfter = '';
    let photos = [];

    while(morePhotosToFetch) {
        let response = await this.fetchImages(photosAfter);

        morePhotosToFetch = response.morePhotosToFetch;
        photosAfter = response.photosAfter;
        photos = [...photos, ...response.assets];
    }

    return photos;
}

async fetchImages(photosAfter) {
    let { edges, page_info } = await CameraRoll.getPhotos({
        /** SHOULD I FETCH SMALLER BATCHES HERE? */
        first: 10000,
        after: photosAfter === '' ? undefined : photosAfter,
        assetType: 'Photos',
    })

    return {
        assets: edges,
        morePhotosToFetch: page_info.has_next_page,
        photosAfter: page_info.end_cursor,
    }
}
4

0 回答 0