Core data 核心数据-与对象(一)和UIImage(多)的一对多关系,存储和获取

Core data 核心数据-与对象(一)和UIImage(多)的一对多关系,存储和获取,core-data,uiimage,one-to-many,fetch,nsmanagedobject,Core Data,Uiimage,One To Many,Fetch,Nsmanagedobject,我的目标是构建一个应用程序,其目标是: -请求(包含唯一id、客户名称、电话、电子邮件、请求信息摘要、图像列表(存储在NSMutable数组中) 我的核心数据包括: 请求 形象 对于我的请求对象,我有: #import <Foundation/Foundation.h> @interface BAPRequest : NSObject @property (nonatomic, retain) NSString *requestID; @property (nonatomi

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

我的核心数据包括:

  • 请求
  • 形象

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

#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
要获取所有请求,我还执行以下操作:

- (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不喜欢它。

看看。这里有一个关于如何使用它的教程:。它会为您省去很多麻烦。

谢谢,但我实际上在尝试学习它,并得到实际的编码答案,这样我就可以申请我目前正在做的许多类似的事情。有时还会尝试使用mogenerator
BAPRequest: NSManagedObject