核心数据 - 与对象(一)和UIImages(许多)的一对多关系,存储和提取

时间:2012-11-15 21:40:53

标签: core-data uiimage one-to-many fetch nsmanagedobject

我的目标是构建一个应用程序,其目标是: - 请求(包含唯一ID,客户名称,电话,电子邮件,请求信息的简短摘要,图像列表(存储在NSMutable数组中)

我的核心数据包含:

  • 请求
  • 图像

Request Object Image Object

对于我的请求对象,我有:

#import <Foundation/Foundation.h>

@interface BAPRequest : NSObject
@property (nonatomic, retain) NSString *requestID;
@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSString *email;
@property (nonatomic, retain) NSString *phone;
@property (nonatomic, retain) NSString *detail;
@property (nonatomic, retain) NSMutableArray *images;
@property (nonatomic, retain) NSDate *requestDate;
@property (nonatomic) BOOL isSent;

- (void)fetchImages;
@end

和.m文件:

#import "BAPAppDelegate.h"
#import "BAPRequest.h"

@implementation BAPRequest
@synthesize name, email, phone, detail;
@synthesize images;
@synthesize requestDate;
@synthesize isSent;
@synthesize requestID;


- (id)init{
  self = [super init];
  if(self != nil){
    self.isSent = false;
    self.requestID = [[NSProcessInfo processInfo] globallyUniqueString];
    self.images = [[NSMutableArray alloc] init];
  }
  return self;
}

- (void)fetchImages{
  //get delegation
  BAPAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];

  NSManagedObjectContext *context = [appDelegate managedObjectContext];

  NSError *error;

  //init request
  NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

  //load the entity description
  NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Image"
                                                       inManagedObjectContext:context];
  //set the request query
  [fetchRequest setEntity:entityDescription];

  //set predicate to request to ask select specific entity with the matching line num
  NSPredicate *pred = [NSPredicate predicateWithFormat:@"(requestId == %@)", self.requestID];
  [fetchRequest setPredicate:pred];

  //exec the command to find the object(table) from core database
  NSArray * imageList = [context executeFetchRequest:fetchRequest error:&error];
  for (NSManagedObject *imageObject in imageList){
    [self.images addObject: [UIImage imageWithData:[imageObject valueForKey:@"content"]]];
  }

}
@end

要获取所有请求,我也这样做:

- (void)removeRequest:(BAPRequest*)request{
    //get delegation
    BAPAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];

    NSManagedObjectContext *context = [appDelegate managedObjectContext];

    NSError *error;

    //init request
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

    //load the entity description
    NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Request"
                                                         inManagedObjectContext:context];
    //set the request query
    [fetchRequest setEntity:entityDescription];

    //set predicate to request to ask select specific entity with the matching line num
    NSLog(@"request id is %@", request.requestID);
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"(requestId LIKE %@)", request.requestID];
    [fetchRequest setPredicate:pred];

    //exec the command to find the object(table) from core database
    NSArray *objects = [context executeFetchRequest:fetchRequest error:&error];

    //if this object doesn't exist in the database, it must be something wrong
    if (objects == nil) {
      NSLog(@"There was an error !");
      NSLog(@"Error Info: %@", error);
    }

    //if object exist in database, take the first search object, and deletes it
    if ([objects count] > 0){
      NSManagedObject *theRequest = [objects objectAtIndex:0];
      [context deleteObject:theRequest];
    }

    [context save:&error];

    /*
    //delete existing images relates to this request first
    entityDescription = [NSEntityDescription entityForName:@"Image"
                                    inManagedObjectContext:context];
    pred = [NSPredicate predicateWithFormat:@"(self.request == %@)", request];
    [fetchRequest setPredicate:pred];
    NSArray * imageList = [context executeFetchRequest:fetchRequest error:&error];
    for (NSManagedObject *removeImageObject in imageList){
      [context deleteObject:removeImageObject];
    }
     */
  }

  #pragma mark - Convert Core Data Object to Request Object

  #pragma mark - Core Data Methods
  - (NSMutableArray *)getRequests{
    //get app delegate
    BAPAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];

    //get object context that's created for us
    NSManagedObjectContext *context = [appDelegate managedObjectContext];

    //define the entities we want load to description
    NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Request"
                                                   inManagedObjectContext:context];

    //init request and set search query to the request
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    [fetchRequest setEntity:entityDescription];

    NSError *error;
    //get objects(table) though request
    NSArray *objects = [context executeFetchRequest:fetchRequest error:&error];

    if (objects == nil){
      NSLog(@"There was an error !");
      //error handling there
      NSLog(@"Error Info: %@", error);
    }

    NSMutableArray *requests = [[NSMutableArray alloc] init];
    for (NSManagedObject *requestObj in objects){
      BAPRequest *request = [[BAPRequest alloc] init];
      request.requestID = [requestObj valueForKey:@"requestId"];
      request.name = [requestObj valueForKey:@"name"];
      request.email = [requestObj valueForKey:@"email"];
      request.phone = [requestObj valueForKey:@"phone"];
      request.detail = [requestObj valueForKey:@"detail"];
      request.requestDate = [requestObj valueForKey:@"requestDate"];
      request.isSent =  [[requestObj valueForKey:@"isSent"] boolValue];

      [request fetchImages];

      [requests addObject: requestObj];
    }

    return requests; //return the requests
  }

  - (void)saveRequest:(BAPRequest *)request{
    request.requestDate = [NSDate date];

    //get delegation
    BAPAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];

    NSManagedObjectContext *context = [appDelegate managedObjectContext];

    NSError *error;

    BAPRequest  *theRequest = [NSEntityDescription insertNewObjectForEntityForName:@"Request"
                                                 inManagedObjectContext:context];


    //set the request obejct basics
    [theRequest setValue:request.requestID forKey:@"requestId"];
    [theRequest setValue:request.name forKey:@"name"];
    [theRequest setValue:request.email forKey:@"email"];
    [theRequest setValue:request.phone forKey:@"phone"];
    [theRequest setValue:request.detail forKey:@"detail"];
    [theRequest setValue:request.requestDate forKey:@"requestDate"];
    [theRequest setValue:[NSNumber numberWithBool:request.isSent ] forKey:@"isSent"];

    //also save its requests
    for (UIImage *img in request.images) {
      //init image object
      UIImage *newImage=[NSEntityDescription insertNewObjectForEntityForName:@"Image" inManagedObjectContext:context];
      [newImage setValue:request.requestID forKey:@"requestId"];
      [newImage setValue:UIImagePNGRepresentation(img) forKey:@"content"];
    }

    [context save:&error];  //save the object
  }
  @end

我的问题是:

我想它根本就不能获取图像。即使他们在getRequests方法中获取了图像,当我尝试将请求传递到其他地方时,它仍然会遇到很多问题:

  • 布尔值未正确加载
  • 图片为零(即使是在获取时加载)

原因是(我可能错了)当我将这些请求对象(BAPRequest *)存储到NSArray并将其传递给表视图时,当我从数组加载它们时(例如[myArray objectAtIndex:0]) ,它们仍然是 NSManagedObject 。我试图在核心数据实体选项中给出一个类名来生成BAPRequest的请求(核心数据)对象类,但它一直说我必须子类化NSManagedObject,其中不可能继承子类NSManagedObject:

我做不到:

BAPRequest: NSManagedObject

XCode不喜欢它。

1 个答案:

答案 0 :(得分:0)

看看mogenerator。这是一个如何使用它的教程:http://raptureinvenice.com/getting-started-with-mogenerator/。它会为你省去很多麻烦。