2

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

该网络服务不遵循 REST 标准,所以我不能使用AFRestClient但有一个子类AFHTTPClientimplements 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、评级、标题、未观看的剧集、剧集计数和年份的属性。我正在像这样映射我HTTPClient的 那些attributesForRepresentation: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"];
}

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

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

谢谢!

4

1 回答 1

1

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

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

(第二部分)

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

于 2012-11-04T23:23:18.097 回答