gpt4 book ai didi

iphone - AFIncrementalStore 处理具有关系的非休息响应

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:47:48 27 4
gpt4 key购买 nike

我正在使用 AFIncrementalStore从网络服务获取数据并通过应用程序启动将其保存在 SQLite 数据库中。

Web 服务不遵循 REST 标准,所以我不能使用 AFRestClient 但有一个 AFHTTPClient 的子类实现了 AFIncrementalStoreHTTPClient

所有数据都适用于第一级数据,但我正在处理的响应还包含具有其他对象的数组:

[
{
"PID": 6,
"Actors": [
{
"PID": 0,
"Name": "Kiefer Sutherland"
},
{
"PID": 0,
"Name": "Carlos Bernard"
},
{
"PID": 0,
"Name": "Mary Lynn Rajskub"
},
{
"PID": 0,
"Name": "Jude Ciccolella"
},
{
"PID": 0,
"Name": "Glenn Morshower"
},
{
"PID": 0,
"Name": "Tanya Wright"
},
{
"PID": 0,
"Name": "Alberta Watson"
},
{
"PID": 0,
"Name": "Eric Balfour"
},
{
"PID": 0,
"Name": "Regina King"
},
{
"PID": 0,
"Name": "Peter MacNicol"
},
{
"PID": 0,
"Name": "Marisol Nichols"
},
{
"PID": 0,
"Name": "Jayne Atkinson"
},
{
"PID": 0,
"Name": "Carlo Rota"
},
{
"PID": 0,
"Name": "Janeane Garofalo"
},
{
"PID": 0,
"Name": "Rhys Coiro"
},
{
"PID": 0,
"Name": "Jeffrey R. Nordling"
},
{
"PID": 0,
"Name": "Bob Gunton"
},
{
"PID": 0,
"Name": "Colm Feore"
},
{
"PID": 0,
"Name": "Annie Wersching"
},
{
"PID": 0,
"Name": "Cherry Jones"
},
{
"PID": 0,
"Name": "D.B. Woodside"
},
{
"PID": 0,
"Name": "James Morrison"
},
{
"PID": 0,
"Name": "Gregory Itzin"
},
{
"PID": 0,
"Name": "Jean Smart"
},
{
"PID": 0,
"Name": "Louis Lombardi"
},
{
"PID": 0,
"Name": "Sarah Clarke"
},
{
"PID": 0,
"Name": "Roger R. Cross"
},
{
"PID": 0,
"Name": "Reiko Aylesworth"
},
{
"PID": 0,
"Name": "Elisha Cuthbert"
},
{
"PID": 0,
"Name": "Lana Parrilla"
},
{
"PID": 0,
"Name": "Kim Raver"
},
{
"PID": 0,
"Name": "Dennis Haysbert"
},
{
"PID": 0,
"Name": "Leslie Hope"
},
{
"PID": 0,
"Name": "Xander Berkeley"
},
{
"PID": 0,
"Name": "James Badge Dale"
},
{
"PID": 0,
"Name": "Penny Johnson Jerald"
},
{
"PID": 0,
"Name": "Sarah Wynter"
},
{
"PID": 0,
"Name": "Shohreh Aghdashloo"
},
{
"PID": 0,
"Name": "Laura Harris"
},
{
"PID": 0,
"Name": "David Anders"
},
{
"PID": 0,
"Name": "Katee Sackhoff"
},
{
"PID": 0,
"Name": "Mykelti Williamson"
},
{
"PID": 0,
"Name": "Anil Kapoor"
},
{
"PID": 0,
"Name": "Chris Diamantopoulos"
},
{
"PID": 0,
"Name": "John Boyd"
},
{
"PID": 0,
"Name": "Freddie Prinze Jr."
}
],
"Artwork": [
{
"Filetype": "jpg",
"Id": "291317649",
"Offset": 0,
"Rating": 1,
"Type": 2
},
{
"Filetype": "jpg",
"Id": "-419936433",
"Offset": 1,
"Rating": 1,
"Type": 2
},
{
"Filetype": "jpg",
"Id": "-431274161",
"Offset": 2,
"Rating": 1,
"Type": 2
},
{
"Filetype": "jpg",
"Id": "117154410",
"Offset": 0,
"Rating": 1,
"Type": 3
},
{
"Filetype": "jpg",
"Id": "1179197275",
"Offset": 1,
"Rating": 1,
"Type": 3
},
{
"Filetype": "jpg",
"Id": "-1912564655",
"Offset": 2,
"Rating": 1,
"Type": 3
},
{
"Filetype": "jpg",
"Id": "885093068",
"Offset": 0,
"Rating": 1,
"Type": 1
}
],
"ContentRating": "TV-14",
"DateAdded": "/Date(-3600000+0100)/",
"EpisodeCount": 192,
"ExternalId": [
{
"Id": "76290",
"Site": "TVDB"
},
{
"Id": "tt0285331",
"Site": "IMDB"
}
],
"Genres": [
"Action and Adventure",
"Drama"
],
"Id": "76290",
"IsProtected": false,
"Rating": 8.9,
"Title": "24",
"UnwatchedEpisodeCount": 0,
"Year": 2001
}
]

在我的数据模型中,我有一个实体 TVShow,它具有 id、评级、标题、未观看的剧集、剧集计数和年份的属性。我将那些映射到我的 HTTPClientattributesForRepresentation:ofEntity:fromResponse:像这样:

if ([entity.name isEqualToString:@"TVShow"]) {
[mutableAttributes setValue:[representation valueForKey:@"Id"] forKey:@"showId"];
[mutableAttributes setValue:[representation valueForKey:@"Title"] forKey:@"title"];
[mutableAttributes setValue:[NSNumber numberWithInteger:[[representation valueForKey:@"Year"] integerValue]] forKey:@"year"];
[mutableAttributes setValue:[NSNumber numberWithInteger:[[representation valueForKey:@"EpisodeCount"] integerValue]] forKey:@"episodeCount"];
[mutableAttributes setValue:[NSNumber numberWithInteger:[[representation valueForKey:@"UnwatchedEpisodeCount"] integerValue]] forKey:@"unwatchedEpisodeCount"];
}

我的想法是,我可以添加另一个实体 Artwork,在 TVShowArtwork 之间具有一对多关系,但我没有'知道如何映射和定义它。我最近几天的所有尝试都导致了崩溃(可能是因为对 CoreData 缺乏了解)。

如能提供任何帮助,我将不胜感激,因为我真的很想在我的应用中使用 AFIS。

谢谢!

最佳答案

(第一部分不是真正的答案)

除非您非常了解 Core Data,否则我不鼓励使用 NSIncrementalStore。使用例如简单地通过网络拉取 JSON 更容易,也更不容易出错。 AFNetwork,然后将这些对象插入到由 SQLite 存储支持的普通核心数据堆栈中。您可能会发现这是一个更可行的解决方案。

(第二部分)

也就是说,您可以让它工作,但您必须采取一些技巧,因为您没有任何 id 或 Artwork 对象的其他唯一标识符。您必须将其放入缓存(AFIncrementalStore 使用的第二个核心数据堆栈),然后将这些对象的 NSManagedObjectID 放入该关系的 TVShow 值中。

关于iphone - AFIncrementalStore 处理具有关系的非休息响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13035815/

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