我正在尝试获取相册的所有照片。我通过网络服务得到这个。 Web服务具有以下布局。
{
"name": "Club Brugge - KRC Genk",
"date": "08.10.2012",
"albumId: 1,
"pictures": [
{
"pic_album_id"=1,
"pic_id" = 1,
"url": "http://www.krcgenk.be/images/gallery/album_199/800X600/1a06dc0e405fd0219e3d327f1eec7fbf.jpg"
},
{
"pic_album_id"=1,
"pic_id" = 2,
"url": "http://www.krcgenk.be/images/gallery/album_199/800X600/e8e10c0664eb0533a0534ed69891b165.jpg"
},
{
"pic_album_id"=1,
"pic_id"= 3,
"url": "http://www.krcgenk.be/images/gallery/album_199/800X600/750b55a87b8eae33b8f3278add9bec44.jpg"
}
]
我有以下功能来获取某张专辑的所有照片。
- (NSMutableArray *)getAllPicturesOfAlbumId: (int)AlbumId
{
NSString *picture_Url = [[NSString alloc]init];
NSArray *results = [[NSArray alloc]init];
_picturesForAlbum = [[NSMutableArray alloc]init];
picture_Url = @"";
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"pic_album_id == %@",
[NSNumber numberWithInt:AlbumId]];
NSLog(@"album id: %@",[NSNumber numberWithInt:AlbumId]);
[request setEntity:[NSEntityDescription entityForName:@"Picture" inManagedObjectContext:self.genkDatabase.managedObjectContext]];
[request setPredicate:predicate];
NSError *error = nil;
results = [self.genkDatabase.managedObjectContext executeFetchRequest:request error:&error];
NSLog(@"results: %@",results);
if (results == nil) {
NSLog(@"nil results");
// handle errors
} else if (results.count == 0) {
NSLog(@"0 results");
// nothing found
} else {
for(int i = 0; i < results.count ; i++){
Picture *picture = [results objectAtIndex:i];
NSLog(@"%@",[results objectAtIndex:i]);
[_picturesForAlbum addObject:picture];
}
}
NSLog(@"%@",_picturesForAlbum);
//NSLog(@"album: %@",[_picturesForAlbum objectAtIndex:5]);
return _picturesForAlbum;
}
上面的代码给出了以下日志。 (我给它测试用例AlbumId = 3)
2012-10-12 14:53:04.577 RacingGenk[4793:c07] album id: 3
2012-10-12 14:53:04.578 RacingGenk[4793:c07] results: (
)
2012-10-12 14:53:04.578 RacingGenk[4793:c07] (
)
希望有人能帮助我。
亲切的问候。
**您在此处看到我的数据库模型的screenshot
修改
在这里,您可以看到我如何获取照片并将其放入我的数据库中。
+ (Picture *)pictureWithGenkInfo:(NSDictionary *)genkInfo
inManagedObjectContext:(NSManagedObjectContext *)context
withAlbumId:(int)albumId
withPictureId:(int)pictureId;
{
Picture *picture = nil;
picture = [NSEntityDescription insertNewObjectForEntityForName:@"Picture"
inManagedObjectContext:context];
picture.url = [genkInfo objectForKey:PICTURES_URL];
picture.pic_album_id = [NSNumber numberWithInt:albumId];
picture.picture_id = [NSNumber numberWithInt:pictureId];
return picture;
}
//在我的第一个控制器中调用上面的方法,该控制器来自一个屏幕。通过这个循环,我填满了我的核心数据库。
for (NSDictionary *genkInfo in albums ) {
albumId++;
[Album albumWithGenkInfo:genkInfo inManagedObjectContext:document.managedObjectContext withAlbumId:albumId];
for (NSDictionary *genkInfo2 in pictures ) {
pictureId++;
[Picture pictureWithGenkInfo:genkInfo2 inManagedObjectContext:document.managedObjectContext withAlbumId:albumId withPictureId:pictureId];
}
pictureId = 0;
// table will automatically update due to NSFetchedResultsController's observing of the NSMOC
}
答案 0 :(得分:0)
使用谓词
[NSPredicate predicateWithFormat:@"whichAlbum.album_id == %d", AlbumId];
更新
for (NSDictionary *genkInfo in albums ) {
albumId++;
Album *album = [Album albumWithGenkInfo:genkInfo inManagedObjectContext:document.managedObjectContext withAlbumId:albumId];
for (NSDictionary *genkInfo2 in pictures ) {
pictureId++;
Picture *pic = [Picture pictureWithGenkInfo:genkInfo2 inManagedObjectContext:document.managedObjectContext withAlbumId:albumId withPictureId:pictureId];
[album addPictureObject:pic]; // this method should be automatically generated
}
pictureId = 0;
// table will automatically update due to NSFetchedResultsController's observing of the NSMOC
}
// don't forget save context
答案 1 :(得分:0)
如何解决问题:
获取自己Liya或其他sqlite数据库查看器。导航到包含核心数据数据库的文件夹。在模拟器上
/Users/<username>/Library/Application Support/iPhone Simulator/<version of iOS>/Applications/<long cryptig string>/<folder-with-the-core-data-database>
打开sqlite
数据库,导航到ZPicture
表并检查数据是否存在。
如果没有数据,那么webservice
- &gt; core-data
程序逻辑存在问题。
如果有数据并且看起来很好,请返回Xcode并启用核心数据请求的日志记录。编辑方案(产品 - 编辑方案),选择Run, Debug
方案并将选项卡更改为Arguments
。在字段Arguments Passed On Launch
中添加-com.apple.CoreData.SQLDebug 3
。确保勾选复选框。
运行你的应用程序,在调试器控制台窗口中应该有core-data生成的sql代码。
您会看到类似于:(您可能会看到whichAlbum
显示)
<timestamp> CoreData: annotation: fetch using NSSQLiteStatement <0xfxxxxxx> on entity
'Picture' with sql text 'SELECT 0, t0.Z_PK, t0.Z_OPT, t0.zpic_album_id
FROM zpicture t0 WHERE t0.zpic_album_id == ?'
<timestamp> CoreData: details: SQLite bind[0] = (int64)3
使用sql命令,返回Liya并将命令粘贴到Run Custom Command
字段中。替换
t0.zpic_album_id == ?
使用您想要查看的album_id
。
查询结果将是Core Data将检索的内容。
不确定whichAlbum
会去哪里。请回过头来告诉我们。