存储数据帮助,不能放入TableView

use*_*231 2 iphone core-data

我遇到了将数据放入表视图的问题.这是我目前的代码.我无法从我尝试使用NSFetchedResultsController的类加载结果,但它不起作用.任何人都可以看到错误.这是头文件.

#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>
#import <sqlite3.h>
#import "FlickrFetcher.h"
@interface PersonList : UITableViewController {
    FlickrFetcher *fetcher;
    NSArray *nameList;
    NSArray *names;
    NSFetchedResultsController *results;
    NSArray *photos;
}
@property (retain, nonatomic)NSArray *nameList;
@property (retain, nonatomic)NSArray *names;
@property (retain, nonatomic)NSArray *photos;
@property (retain, nonatomic)NSFetchedResultsController *results;
@end
Run Code Online (Sandbox Code Playgroud)

这是.m文件.

#import "PersonList.h"


@implementation PersonList
@synthesize nameList,names,results,photos;

 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
      self.title=@"Contacts";
        // Custom initialization
    }
    return self;
}


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    fetcher = [FlickrFetcher sharedInstance];
    NSString *path=[[NSBundle mainBundle]pathForResource:@"FakeData" ofType:@"plist"];
    NSArray *array=[NSArray arrayWithContentsOfFile:path];
    NSManagedObjectContext *context=[fetcher managedObjectContext];
    if([fetcher databaseExists]==YES){
        for(NSDictionary *dic in array){
            PersonList *person=(PersonList *)[NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:context];
            [person setNameList:[dic objectForKey:@"user"]];
            [person setPhotos:[dic objectForKey:@"path"]];
            names=[fetcher fetchManagedObjectsForEntity:@"Person" withPredicate:nil];
            results=[fetcher fetchedResultsControllerForEntity:@"Person" withPredicate:nil];
        }       
    }
    [super viewDidLoad];
}


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    self.names=nil;
}


- (void)dealloc {
    [fetcher release];
    [nameList release];
    [super dealloc];
}
#pragma mark -
#pragma mark Table View Data Source Methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [[results sections] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"NOT REACHED HERE");
    static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
                             SimpleTableIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc]
                 initWithStyle:UITableViewCellStyleDefault
                 reuseIdentifier:SimpleTableIdentifier] autorelease];
    }
    NSUInteger row = [indexPath row];
    cell.textLabel.text = [[results sections] objectAtIndex:row];
    return cell;
}
@end
Run Code Online (Sandbox Code Playgroud)

这是FlickrFetcher方法的方法

#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>

@interface FlickrFetcher : NSObject {
    NSManagedObjectModel *managedObjectModel;
    NSManagedObjectContext *managedObjectContext;
    NSPersistentStoreCoordinator *persistentStoreCoordinator;
}

// Returns the 'singleton' instance of this class
+ (id)sharedInstance;

// Checks to see if any database exists on disk
- (BOOL)databaseExists;

// Returns the NSManagedObjectContext for inserting and fetching objects into the store
- (NSManagedObjectContext *)managedObjectContext;

// Returns an array of objects already in the database for the given Entity Name and Predicate
- (NSArray *)fetchManagedObjectsForEntity:(NSString*)entityName withPredicate:(NSPredicate*)predicate;

// Returns an NSFetchedResultsController for a given Entity Name and Predicate
- (NSFetchedResultsController *)fetchedResultsControllerForEntity:(NSString*)entityName withPredicate:(NSPredicate*)predicate;

@end
Run Code Online (Sandbox Code Playgroud)

Mar*_*rra 10

  1. [super viewDidLoad]在您的代码之前拨打电话.只有在您的代码出现在-dealloc方法之后才能调用super .
  2. 你正在循环数组,得到一个NSFetchedResultsController并且什么都不做.你甚至没有参考.这是不正确的.你应该有ONE NSFetchedResultsControllerUITableViewController; 你应该保留对它的引用; 你的UITableViewDatasource(在这种情况下是你的UITableViewController)应该是它的代表.
  3. 您没有调用-performFetch:,NSFetchedResultsController因此没有数据被检索.
  4. 因为您不是代理人,所以NSFetchedResultsController无论如何都无法知道它何时返回数据,因为这是传达数据更改的单一方式.

我会重新考虑您的设计并再次审核Core Data iPhone示例项目.

更新

NSFetchedResultsController您保存之前不会触发其委托方法NSManagedObjectContext,这就是您没有看到数据的原因.

至于你得到的错误,你试图在一个不响应该属性的对象上设置属性.我建议将您的应用程序加载到调试器中,放置断点objc_exception_throw并查看导致问题的操作对象.你很可能会找回一个物体并认为它是另一个物体.