0

I aim to build a app with the object of: - Request(contains an unique id, customer name, phone, email, a brief summary of request info, a list of images(store in NSMutable Array)

My Core data contains:

  • Request
  • Image

Request Object Image Object

For my request object, I have:

#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

And the .m File:

#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

To fetch all the requests, I also do:

- (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

My Issue is:

I guess it just doesn't fetch the images at all. Even they fetched the images in the getRequests method, when I try to pass the request to somewhere else, it still get lots issues:

  • boolean not loading properly
  • images are nil(even it was loaded when fetch)

and the reason is(I can be wrong) that when I store these request objects(BAPRequest*) into a NSArray and pass it to a table view, when I load them from array (e.g [myArray objectAtIndex:0]), they are still NSManagedObject. I tried to give a class name in the core data entity option to make the Request(Core Data) Object Class of BAPRequest, but it keeps saying I have to subclass NSManagedObject, where subclassing NSManagedObject is not possible:

I can't do:

BAPRequest: NSManagedObject

XCode doesn't likes it.

phil88530
  • 1,499
  • 3
  • 19
  • 27

1 Answers1

0

Look at mogenerator. Here's a tutorial on how to use it: http://raptureinvenice.com/getting-started-with-mogenerator/ . It will save you a lot of trouble.

Matt Long
  • 24,438
  • 4
  • 73
  • 99
  • thank you, but I actually trying to learn it and get the actualy coding answer so I can apply for many similar things I am currently doing. Still will have a try of mogenerator sometimes – phil88530 Nov 16 '12 at 15:47