AlamoFire URLRequest httpBody 数据未发布

AlamoFire URLRequest httpBody data is not released

提问人:YosiFZ 提问时间:10/29/2023 最后编辑:YosiFZ 更新时间:10/29/2023 访问量:40

问:

我有一个 iOS 项目,在应用程序的共享扩展中使用 AlamoFire 发送文件。

这是我用于将数据发送到服务器的代码:

func msgRequest(message:MessageData, 
                    from:[Any],
                    groupId:NSNumber?,
                    groupName:String?,
                    lastServerId:String?,
                    completion: @escaping (SESendResponse) -> ()) -> Int32 {
        
        var request = URLRequest(url: URLSEND)
        request.httpMethod = HTTPMethod.post.rawValue
        request.setValue("application/json", forHTTPHeaderField: "Content-Type")

        //In this function we create NSData with all the data(file,image, video)
        request.httpBody = SendMsgData.addPost(message, from: from, groupId: groupId, groupName: groupName, lastServerId: lastServerId)
        
        currentCommandHandlerID += 1
        let requestCommandID = currentCommandHandlerID

        session.request(request).responseJSON { (response) in
            request.httpBody = nil
            
            switch response.result {
            case .success(let success):
                if let jsonArray = success as? [Any],
                   let jsonDic = jsonArray.first as? [String:Any],
                    let messageID = jsonDic["messageID"] as? NSNumber,
                    let resultCode = jsonDic["resultCode"] as? NSNumber {
                    let res = SESendResponse(reqId: requestCommandID, msgId: "\(messageID)", successed: true, abort: false)
                    
                    completion(res)
                }
            case .failure(let failure):
                break
            } 
        }
        
        return requestCommandID
    }

当一个文件上传完我想发送下一个文件时,问题是 HTTPBody 没有释放并保留在内存中。

例如,在文件的第一次迭代中,内存使用量为 28 MB,HTTPBody 为 25MB(总内存使用量 = 53),当上传完成并开始一个新文件时(我发送相同的文件),内存使用量增加到 78(因此第一个数据被释放)。

知道可能是什么问题吗?

编辑


这是其他方法:

+(NSData*) addPostData:(MessageData*)message from:(NSArray*)from groupId:(NSNumber* _Nullable)groupId groupName:(NSString* _Nullable)groupName lastServerId:(NSString* _Nullable) lastServerId {
    NSArray* extraData = nil;
    if(groupId.integerValue > 0) {
        extraData = [NSArray arrayWithObjects:groupName, lastServerId, nil];
    } else if([[message globalGroupsIdArray] count]) {
        extraData =  [NSArray arrayWithObjects:lastServerId, nil];
    } else {
        extraData = [NSArray arrayWithObjects:from, lastServerId, nil];
    }
    
    NSMutableArray* arrrecipient = [NSMutableArray array];
    
    if(message.groupId.integerValue == 0 && message.globalGroupsIdArray.count == 0) {
        for (NSString* value in [extraData objectAtIndex:0]) {
            NSString* formattedNumber = [CommonUtils formatNumberForMessageServer:value];
            [arrrecipient addObject:[NSDictionary dictionaryWithObjectsAndKeys :formattedNumber,@"*****",@"*****",@"*****",@"*****",@"*****",@"*****",@"*****",nil]];
        }
    } else if(message.groupId.integerValue > 0) {
        NSDictionary* dic = [NSDictionary dictionaryWithObjectsAndKeys :[extraData objectAtIndex:0] ,@"*****",@"*****",@"*****",@"*****",@"*****",@"*****",@"*****",@"*****",@"*****",[NSNumber numberWithBool:YES],@"isGroupSenderExcluded",message.groupId.stringValue,@"*****",[extraData objectAtIndex:0],@"*****",nil];
        [arrrecipient addObject:dic];
    } else {
        for(int i = 0;i < [message.globalGroupsIdArray count] ; i++) {
            NSNumber* gId = [message.globalGroupsIdArray objectAtIndex:i];
            NSDictionary* dic = [NSDictionary dictionaryWithObjectsAndKeys :
                                 @"*****",@"*****",
                                 @"*****",@"*****",
                                 @"*****",@"*****",
                                 @"*****",@"*****",
                                 [NSNumber numberWithBool:NO],@"*****",
                                 [NSNumber numberWithBool:NO],@"*****",
                                 [gId stringValue],@"*****",
                                 @"*****",@"*****",
                                 @"" ,@"*****",
                                 nil];
            [arrrecipient addObject:dic];
        }
    }
    
    NSMutableDictionary* messageDictionary = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@"*****",@"*****" ,message.message,@"*****",arrrecipient,@"*****", nil];
    
    if ([[ConfigUtils shareInstance] alwaysSetReplyToMessage]) {
        if(extraData.count > 1) {
            [messageDictionary setObject: [NSNumber numberWithInt:(int)[[extraData objectAtIndex:1] integerValue]] forKey:@"*****"];
            [messageDictionary setObject:@"*****" forKey:@"*****"];
        }
    } else if([message.replyMessageId length]) {
        [messageDictionary setObject: message.replyMessageId forKey:@"*****"];
        [messageDictionary setObject:@"*****" forKey:@"*****"];
    }
    
    if(message.mmsBubblesArray.count > 0) {
        MMSObject* mmsObject = message.mmsBubblesArray[0];
        @autoreleasepool {
            NSArray* fileMessages = [self addMultimediaFile:message.mmsBubblesArray];
            [messageDictionary setObject:fileMessages forKey:@"*****"];
        }
    }
    
    if(message.uuid.length > 0) {
        [messageDictionary setValue:message.uuid forKey:@"*****"];
        [messageDictionary setObject:[NSNumber numberWithBool:message.isRetry] forKey:@"*****"];
    }
    
    NSArray *arr = [[NSArray alloc] initWithObjects:[CommonUtils getIPAuthenticationDetails],messageDictionary,nil];
    messageDictionary = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:arr
                                                       options:NSJSONWritingPrettyPrinted
                                                         error:nil];
    arr = nil;
    
    return jsonData;
}

+(NSArray*) addMultimediaFile:(NSArray*)mmsBubblesArray {
    NSMutableDictionary* dic = [NSMutableDictionary dictionary];
    NSArray* array = [NSArray arrayWithObject:dic];
    
    [dic setObject:@"****" forKey:@"****"];
    [dic setObject:[NSNumber numberWithInt:0] forKey:@"****"];
    
    NSString* fileName = nil;
    NSString* value = nil;
    MMSObject *mmsObject  = [mmsBubblesArray objectAtIndex:0];
    
    NSData* data = [[NSData alloc] initWithContentsOfURL:mmsObject.fileURL];
    fileName = mmsObject.fileName;
    value = [data base64EncodedStringWithOptions:0];

    [dic setObject:mmsObject.mimeType forKey:@"****"];
    [dic setObject:fileName forKey:@"*****"];
    
    if (value) {
        [dic setObject:value forKey:@"****"];
    }
    
    return array;
}
iOS版 斯威夫特 阿拉莫火

评论

0赞 Jon Shier 10/29/2023
应该添加的实现,这似乎是相关的。addPost
0赞 YosiFZ 10/29/2023
@JonShier我使用相关代码更新我的问题
0赞 Jon Shier 10/30/2023
呜我建议你使用 Xcode 的内存调试器来查看请求后保留数据的内容。我认为不太可能是阿拉莫菲尔。
0赞 Larme 10/30/2023
如果您不使用 Alamofire 代码,但只是这样做,那么它似乎是泄漏吗?let data = SendMsgData.addPost(...)

答: 暂无答案