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

该Web服务不遵循REST标准,因此我不能使用AFRestClient,但是有一个实现AFHTTPClientAFIncrementalStoreHTTPClient子类。

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

[
{
    "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。只需使用例如AFNetwork,然后将这些对象插入由SQLite存储支持的普通Core Data堆栈中。您可能会发现一个更可行的解决方案。

(第二部分)

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

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

10-09 06:04