我正试着弄到一张相册的所有照片。我是通过网络服务得到这个的。webservice具有以下布局。
{
"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] (
)希望有人能帮我。
致以亲切的问候。
**在这里您可以看到我的数据库模型的屏幕截图
编辑
在这里你可以看到我是如何得到我的照片并把它放在我的数据库里的。
+ (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
}发布于 2012-10-12 21:42:26
使用谓词
[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发布于 2012-10-14 09:36:46
如何排除下列故障:
获取李雅或另一个sqlite数据库查看器。导航到具有核心数据数据库的文件夹。在模拟器上
/Users/<username>/Library/Application Support/iPhone Simulator/<version of iOS>/Applications/<long cryptig string>/<folder-with-the-core-data-database>
打开sqlite数据库,导航到ZPicture表并检查数据是否存在。如果没有数据,那么webservice -> core-data程序逻辑就有问题了。
如果有数据并且看起来很好,请返回Xcode并为核心数据请求启用日志记录。编辑方案(Product方案),选择Run, Debug方案并将选项卡更改为Arguments。在字段中,Arguments Passed On Launch添加-com.apple.CoreData.SQLDebug 3。确保勾选复选框。
运行应用程序,在调试器控制台窗口中应该有由核心数据生成的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数据将检索的内容。
不知道whichAlbum会去哪里。带着你的发现回来告诉我们。
https://stackoverflow.com/questions/12859622
复制相似问题