gpt4 book ai didi

ios - AlamofireCoreData Pod

转载 作者:行者123 更新时间:2023-11-30 11:58:33 25 4
gpt4 key购买 nike

我正在尝试使用 AlamofireCoreData Pod,但遇到了问题。每个值都为零; (请参阅下面的控制台日志消息)

这是我的核心数据,新闻类

extension News {

@nonobjc public class func fetchRequest() -> NSFetchRequest<News> {
return NSFetchRequest<News>(entityName: "News")
}

@NSManaged public var content: String?
@NSManaged public var created_at: NSDate?
@NSManaged public var date_pub_beg: NSDate?
@NSManaged public var date_pub_end: NSDate?
@NSManaged public var id: Int16
@NSManaged public var results: Bool
@NSManaged public var rotation: Bool
@NSManaged public var summary: String?
@NSManaged public var title: String?
@NSManaged public var updated_at: NSDate?

}

在我的 swift 文件中我这样做

private var appDelegate = UIApplication.shared.delegate as! AppDelegate
private let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext

func getNews(url: String, completion: @escaping (Bool) -> Void) {

Alamofire.request(url).responseInsert(context: context, type: Many<News>.self) { response in
switch response.result {
case let .success(news):
print (news)
completion(true)
case .failure:
// handle error
completion(false)

print("Erreur")
}
}
}

在控制台日志中调用我的 func print this,对 api 的调用似乎可以工作,但似乎没有数据被解析。

[<News: 0x60800009fef0> (entity: News; id: 0x60800003c880 <x-coredata:///News/tBB28D896-033A-4084-807A-6A31993B8F2F2> ; data: {
content = nil;
"created_at" = nil;
"date_pub_beg" = nil;
"date_pub_end" = nil;
id = 0;
results = nil;
rotation = nil;
summary = nil;
title = nil;
"updated_at" = nil;
}), <News: 0x608000282170> (entity: News; id: 0x60800003ba00 <x-coredata:///News/tBB28D896-033A-4084-807A-6A31993B8F2F3> ; data: {
content = nil;
"created_at" = nil;
"date_pub_beg" = nil;
"date_pub_end" = nil;
id = 0;
results = nil;
rotation = nil;
summary = nil;
title = nil;
"updated_at" = nil;
})]

这是我的 API 发送的内容,JSON REST API 使用 Rails 制作。

[
{
id: 2,
title: "Victoire de la France",
date_pub_beg: "2017-11-27",
date_pub_end: "2017-12-31",
summary: "La France remporte la Coupe Davis.",
content: "<div>Au terme du dernier match, la France remporte la <strong>Coupe Davis.</strong></div>",
created_at: "2017-11-27T14:18:07.527Z",
updated_at: "2017-11-27T14:18:07.527Z",
results: false,
rotation: false
},
{
id: 1,
title: "Test d'article",
date_pub_beg: "2017-09-05",
date_pub_end: "2017-09-06",
summary: "Ceci est un test, AH BON ?",
content: "<div><strong>Petit test pour voir !</strong></div><ul><li>test 1</li><li>test 2</li><li>test 3</li></ul><div><br></div>",
created_at: "2017-09-05T07:43:18.011Z",
updated_at: "2017-11-27T14:42:18.907Z",
results: false,
rotation: false
}
]

一切似乎都很好,不明白为什么那些零值。

有什么想法吗?

非常感谢

最佳答案

我使用的 Alamofire 有点不同,但效果很好。我从来没有像你一样见过这个实现。试试这个

func getNews(url: String, completion: @escaping (Bool) -> Void)  {        
var alamoFireManager : Alamofire.SessionManager?
alamoFireManager?.request(url, method: .get, encoding: JSONEncoding.default, headers:nil)
.responseJSON { (response) in
switch response.result {
case .success:
print(response.result.value!)
completion(true)
case .failure(let error):
print(error.localizedDescription)
completion(false)
}
}
}

如果您有一些身份验证 header ,您可以将其作为数组放置在 headers 参数中["Authorization":yourkey]

作为补充,我使用 alamoFireManager 作为单例(全局)。

我有这个扩展来帮助调试:

extension Request {
public func debugLog() -> Self {
#if DEBUG
debugPrint(self)
#endif
return self
}
}

用途:

func getNews(url: String, completion: @escaping (Bool) -> Void)  {        
var alamoFireManager : Alamofire.SessionManager?
alamoFireManager?.request(url, method: .get, encoding: JSONEncoding.default, headers:nil)
.debugLog() // <<<-----
.responseJSON { (response) in
switch response.result {
case .success:
print(response.result.value!)
completion(true)
case .failure(let error):
print(error.localizedDescription)
completion(false)
}
}
}

它将打印您的请求

好机会

关于ios - AlamofireCoreData Pod,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47513688/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com