提问人:YosiFZ 提问时间:10/29/2023 最后编辑:YosiFZ 更新时间:10/29/2023 访问量:40
AlamoFire URLRequest httpBody 数据未发布
AlamoFire URLRequest httpBody data is not released
问:
我有一个 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;
}
答: 暂无答案
评论
addPost
let data = SendMsgData.addPost(...)