0

我正在尝试获取相册的所有图片。我通过网络服务得到这个。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] (
)

希望有人可以帮助我。

亲切的问候。

** 这里你可以看到我的数据库模型的截图

编辑

在这里,您会看到我如何获取照片并将其放入数据库中。

+ (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
            }
4

2 回答 2

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
于 2012-10-12T21:42:26.487 回答
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->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会去哪里。只需返回您的发现并告诉我们。

于 2012-10-14T09:36:46.773 回答