0

私は次の目的でアプリを構築することを目指しています:

私のコア データには以下が含まれます。

  • リクエスト
  • 画像

リクエストオブジェクト 画像オブジェクト

私のリクエストオブジェクトには、次のものがあります。

#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 の Request(Core Data) オブジェクト クラスを作成しようとしましたが、NSManagedObject のサブクラス化が不可能な場合、NSManagedObject をサブクラス化する必要があると言い続けています。

私はできません:

BAPRequest: NSManagedObject

XCodeはそれが好きではありません。

4

1 に答える 1

0

mogeneratorを見てください。使用方法のチュートリアルは次のとおりです: http://raptureinvenice.com/getting-started-with-mogenerator/。それはあなたに多くの手間を省きます。

于 2012-11-16T14:30:35.457 に答える