NativeScript / nativescript-background-http

Background Upload plugin for the NativeScript framework
Apache License 2.0
101 stars 50 forks source link

Performance issue on multiple upload #187

Closed closca closed 5 years ago

closca commented 5 years ago

Make sure to check the demo app(s) for sample usage

Make sure to check the existing issues in this repository

If the demo apps cannot help and there is no issue for your problem, tell us about it

Please, ensure your title is less than 63 characters long and starts with a capital letter.

Which platform(s) does your issue occur on?

IOS

Please, provide the following version numbers that your issue occurs with:

Is there any code involved?

imageSourceModule.fromAsset(imageAsset).then(imageSource => {
            const folder = fileSystemModule.knownFolders.documents().path;
            const fileName = 'tes.png';
            const path = fileSystemModule.path.join(folder, fileName);
            const saved = imageSource.saveToFile(path, 'png');
            if (saved) {
                let file = fileSystemModule.File.fromPath(path);
                let request = {
                    url: ENV.API_HOST + '/api/v1/upload',
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/octet-stream',
                        'File-Name': file._name,
                        Origin: ENV.HTTP_ORIGIN,
                        Authorization: `Bearer ${APP_SETTINGS.getString(ENV.AUTH_TOKEN_KEY)}`
                    },
                    description: 'upload'
                };

                let params = [ {
                        name: 'file',
                        filename: file._path,
                        mimeType: mime
                    }];

                let task = imageUploadSession.multipartUpload(params, request);
                task.on('error', response => {
                    console.log(response);
                });
                task.on('responded', (response: IHttpBackgroundOnResponse) => {
                    console.log(response);
                });
            } else {
                alert('Failed to upload image. Try again!');
            }
        });

I suspect that the most expensive part is when is saving the file in the knowFolders. I tried to upload the image directly from gallery without saving in knownFolders but is sending an empty file or something because I can see on server side this mime: node/x-empty

Here is my code for direct upload:

context.authorize()
            .then(() => {
                return context.present();
            })
            .then((selection: ImageAsset[]) => {
                selection.forEach((selectedImage: ImageAsset) => {
                    const iosSelected = selectedImage.ios;
                    let selectedImagePath;
                    if (iosSelected && iosSelected.mediaType === PHAssetMediaType.Image) {
                        const opt = PHImageRequestOptions.new();
                        opt.version = PHImageRequestOptionsVersion.Current;
                        PHImageManager.defaultManager().requestImageDataForAssetOptionsResultHandler(
                            iosSelected, opt, (imageData: NSData, dataUTI: string, orientation: UIImageOrientation, info: any) => {
                                selectedImagePath = info.objectForKey('PHImageFileURLKey').toString();
                                let extension = selectedImagePath.split('.').pop().toLowerCase();
                                let imageBaseName = baseName(selectedImagePath) + '.' + extension;
                                let request = {
                                    url: ENV.API_HOST + '/api/v1/upload',
                                    method: 'POST',
                                    headers: {
                                        'Content-Type': 'application/octet-stream',
                                        'File-Name': imageBaseName,
                                        Origin: ENV.HTTP_ORIGIN,
                                        Authorization: `Bearer ${APP_SETTINGS.getString(ENV.AUTH_TOKEN_KEY)}`
                                    },
                                    description: 'upload'
                                };
                                let params = [{
                                        name: 'file',
                                        filename: selectedImagePath,
                                        mimeType: mime
                                    }];

                                let task = imageUploadSession.multipartUpload(params, request);
                                });

                            });
                    } 
                });
            });
tbozhikov commented 5 years ago

Hi @closca, Check out this demo for uploading images. Tested it in Xcode and Simulator, profiled the memory and did not see any excessive memory usage. Note, that it uses calls to native iOS libs for storing the file to the knownFolders.

Let us know if this works for you.

elena-p commented 5 years ago

Closing the issue due to inactivity