2

Bluetooth 経由で外部アクセサリから最大 2MB を読み取ろうとしていますが、予想よりもはるかに時間がかかります。外部アクセサリ側で最適化していますが、データを s3 にアップロードする前に一時ファイルに書き込むより効率的な方法、または入力ストリームを直接 s3 にパイプする方法も探しています。

現時点では、EADemo コードと aws の例にほぼ基づいた非常に単純なメカニズムがあります。

// low level read method - read data while there is data and space available in the input buffer
- (void)_readData {
#define INPUT_BUFFER_SIZE 1024
    uint8_t buf[INPUT_BUFFER_SIZE];
    while ([[_session inputStream] hasBytesAvailable])
    {
        NSInteger bytesRead = [[_session inputStream] read:buf maxLength:INPUT_BUFFER_SIZE];
        if (_readData == nil) {
            _readData = [[NSMutableData alloc] init];
        }
        [_readData appendBytes:(void *)buf length:bytesRead];
    }

    [[NSNotificationCenter defaultCenter] postNotificationName:SessionDataReceivedNotification object:self userInfo:nil];
}

そして、_sessionDataReceived:

- (void)_sessionDataReceived:(NSNotification *)notification
{
    SessionController *sessionController = (SessionController *)[notification object];
    uint32_t bytesAvailable = 0;
    NSData *streamData;

    while ((bytesAvailable = [sessionController readBytesAvailable]) > 0)
    {
        streamData = [sessionController readData:bytesAvailable];
    }

    if (![_fileMgr fileExistsAtPath:_deviceFile]) {
        [_fileMgr createFileAtPath:_deviceFile contents:nil attributes:nil];
    }

    NSFileHandle *fileHandle = [NSFileHandle fileHandleForUpdatingAtPath:_deviceFile];
    [fileHandle seekToEndOfFile];
    [fileHandle writeData:streamData];
    [fileHandle closeFile];

    NSDictionary *attributes = [_fileMgr attributesOfItemAtPath:_deviceFile error:NULL];
    unsigned long long fileSize = [attributes fileSize];

    if (fileSize == (ourExpectedBytesSize)) {
        [self sendToS3];
    }
}

sendToS3:

- (void)sendToS3
{    
    _s3filekey = [NSString stringWithFormat:@"%@/files/%@", user, _deviceFilename];

    AmazonS3Client *s3 = [self s3Client];

    S3TransferManager *tm = [S3TransferManager new];
    tm.delegate = self;
    tm.s3 = s3;

    S3PutObjectRequest *por = [[S3PutObjectRequest alloc] initWithKey:_s3filekey inBucket:_uploadBucket];
    por.requestTag = @"sendToS3";
    por.filename = _deviceFile;

    [tm upload:por];
}

このデータをできるだけ速く/効率的に取得して s3 にプッシュする最良の方法は何でしょうか?

4

1 に答える 1

0

バックグラウンド スレッドで S3 にデータを送信してみることをお勧めします。

于 2013-06-16T01:13:26.830 に答える