在 badoo/Chatto 聊天文本上填充 api 数据时,无法访问 alamofire 请求范围之外的模型对象数据
Posted
技术标签:
【中文标题】在 badoo/Chatto 聊天文本上填充 api 数据时,无法访问 alamofire 请求范围之外的模型对象数据【英文标题】:Can't access model object data outside the alamofire request scope while populating api data on badoo/Chatto chat text 【发布时间】:2018-07-15 15:15:19 【问题描述】:以前我成功地访问了类中任何地方的模型对象,但是在 badoo/chatto 文本视图上填充数据时我被卡住了。 我正在将聊天消息 api 集成到 badoo 聊天视图中
基本上,问题是 alamofire 响应没有超出范围。
我尝试过编译处理程序但没有运气吗?有没有办法解决这个问题?
提前致谢。
这里是代码 sn-p:
import Foundation
import Chatto
import ChattoAdditions
import SwiftyJSON
import Alamofire
class DemoChatMessageFactory
public static var chats = [ChatModel]()
class func makeMessage(_ uid:String) -> DemoTextMessageModel
print("uid makeMessage : \(uid)")
return self.makeMessageData(uid, isIncoming:false)
class func makeMessageData(_ uid: String,isIncoming:Bool) -> DemoTextMessageModel
if isIncoming == true
return self.makeTextFinalMessage(uid, isIncoming:isIncoming)
else
return self.makeTextFinalMessage(uid, isIncoming: isIncoming)
public class func makeTextMessage(_ uid: String, isIncoming: Bool,text:String) -> DemoTextMessageModel
let messageModel = self.makeMessageModel(uid, isIncoming: isIncoming,
type: TextMessageModel<MessageModel>.chatItemType)
let textMessageModel = DemoTextMessageModel(messageModel:messageModel,
text: text)
return textMessageModel
public class func makeTextFinalMessage(_ uid: String, isIncoming: Bool) -> DemoTextMessageModel
var text = String()
var uidInt = Int(uid)
print("string uid 121 \(uid)")
print("print is Incomming data or not 1: \(isIncoming)")
print("uid count :\(uid.count)")
let urlString = "[My message Api]"
Alamofire.request(urlString, method: .get).validate().responseJSON
(response) -> Void in
if let value = response.data
do
let json = try JSON(data: value)
if let dictionnary = json.dictionaryObject
if let messageArray = dictionnary["message"] as?[[String: Any]]
self.chats.removeAll()
for arr in messageArray
self.chats.append(ChatModel(ChatListJSON: arr))
catch
print("cannot convert to Json")
print("print int 122 : \(uidInt!)")
print("Chat List Id DemoChatMessageFactory \(self.chats[uidInt!].chatId)")
print("chat message: \(String(describing: uidInt!)) th \(self.chats[uidInt!].chatMessage)")
self.textData = "\(self.chats[uidInt!].chatMessage)"
self.makeTextMessage(uid, isIncoming: isIncoming, text:self.textData) //Here I am bale to pass textData but ouside the Alamofire block can't access
//Here getting empty values
print("uid makeTextFinalMessage \(uid)")
print("in coming makeTextFinalMessage \(isIncoming)")
print("text makeTextFinalMessage \(text)")
//chat count also getting zero count
print("chat count final text\(chats.count)")
print("print chat count : \(self.chats.count)")
return self.makeTextMessage(uid, isIncoming: isIncoming, text:self.textData)
测试完成处理程序
public var res: Any = ""
func getAllChatData(completionhandler:@escaping ([String: Any]?) -> ())
let URL = "my api"
Alamofire.request(URL).responseJSON
response in
if let json = response.result.value as? [String: Any]
completionhandler(json, nil)
else if let error = response.result.error as Error?
completionhandler(nil, error)
并在函数内部使用如下调用
DemoChatMessageFactory.getAllChatData
(result) in
res = result
print("response (res)")
请建议我使用编译处理程序进行 alamofire 的正确方法
【问题讨论】:
你的意思是 completion handler 和 compilation handler 吗?如果您说您尝试过,请使用完成处理程序显示您的代码。 public var res: Any = "" func getAllChatData(completionhandler:@escaping ([String: Any]?) -> ()) let URL = "my api" Alamofire.request(URL) .responseJSON response in if let json = response.result.value as? [String: Any] completionhandler(json, nil) else if let error = response.result.error as Error? 完成处理程序(无,错误) 你最好给你的代码一个更好的格式。无论如何,你能从你的print("response (res)")
得到输出吗?
似乎已经成为添加cmets的极限了。无论如何,您只需要在完成处理程序中使用结果。如果你在makeTextFinalMessage
内部调用它,它需要有一个完成处理程序,而你的makeMessageData
,makeMessage
,所有这些东西都需要使用完成处理程序。
【参考方案1】:
这是一个使用异步调用结果转换所有方法的示例。由于我从未使用过Chatto
,并且您没有在代码中显示所有类型,因此您可能需要修改我的代码的许多部分,但我相信您可以看到您需要使用此代码做什么。
import Foundation
import Chatto
import ChattoAdditions
import SwiftyJSON
import Alamofire
class DemoChatMessageFactory
public static var chats = [ChatModel]()
class func requestMessage(_ uid:String,
completion: @escaping (DemoTextMessageModel?, Error?)->Void)
print("uid makeMessage : \(uid)")
self.requestMessageData(uid, isIncoming: false) (model, error) in
completion(model, error)
class func requestMessageData(_ uid: String, isIncoming: Bool,
completion: @escaping (DemoTextMessageModel?, Error?)->Void)
if isIncoming
//...put any code needed when isIncoming is true
self.requestTextFinalMessage(uid, isIncoming: isIncoming) model in
completion(model, error)
else
//...put any code needed when isIncoming is false
self.requestTextFinalMessage(uid, isIncoming: isIncoming) model in
completion(model, error)
public class func makeTextMessage(_ uid: String, isIncoming: Bool, text: String) -> DemoTextMessageModel
let messageModel = self.makeMessageModel(uid, isIncoming: isIncoming,
type: TextMessageModel<MessageModel>.chatItemType)
let textMessageModel = DemoTextMessageModel(messageModel:messageModel,
text: text)
return textMessageModel
public class func requestTextFinalMessage(_ uid: String, isIncoming: Bool,
completion: @escaping (DemoTextMessageModel?, Error?)->Void)
var text = String()
var uidInt = Int(uid)
print("string uid 121 \(uid)")
print("print is Incomming data or not 1: \(isIncoming)")
print("uid count :\(uid.count)")
let urlString = "[My message Api]"
Alamofire.request(urlString, method: .get).validate().responseJSON
(response) -> Void in
if let value = response.data
do
let json = try JSON(data: value)
if let dictionnary = json.dictionaryObject
if let messageArray = dictionnary["message"] as?[[String: Any]]
self.chats.removeAll()
for arr in messageArray
self.chats.append(ChatModel(ChatListJSON: arr))
print("print int 122 : \(uidInt!)")
print("Chat List Id DemoChatMessageFactory \(self.chats[uidInt!].chatId)")
print("chat message: \(String(describing: uidInt!)) th \(self.chats[uidInt!].chatMessage)")
self.textData = "\(self.chats[uidInt!].chatMessage)"
completion(self.makeTextMessage(uid, isIncoming: isIncoming, text: self.textData), nil)
catch
print("cannot convert to Json")
completion(nil, error)
else
//better generate an error case result, and call completion.
//...
我将一些方法名称从 make...
更改为 request...
以表明它们是异步方法。
以及用法,如果您打算将原始代码用作:
let model = DemoChatMessageFactory.makeMessage(uid)
//Do some UI updates using `model`...
您可能需要使用异步方法,例如:
DemoChatMessageFactory.requestMessage(uid) (model, error) in
if let model = model
//Do some UI updates using `model`...
else
//Do something for the error...
【讨论】:
感谢您帮助我。我解决了这个问题。 Badoo pod 限制访问外部值。我在 Badoo 类中初始化了模型对象以上是关于在 badoo/Chatto 聊天文本上填充 api 数据时,无法访问 alamofire 请求范围之外的模型对象数据的主要内容,如果未能解决你的问题,请参考以下文章