ios - 推送查看时核心数据崩溃
<p><p>所以这是我第一次使用核心数据,到目前为止还不是最好的体验。到目前为止,我的应用程序由两个 UITableViewController 和一个 ViewController 组成。该应用程序只是要求用户在 UIAlert 上输入列表的名称,然后将其保存到核心数据中,然后将列表的名称放入第一个 tableview 中。到目前为止,一切都很好。然后用户单击列表的名称并将它们推送到列表上的内容,该列表是空的,因为它还没有被填充。所以我的问题是当我去填充它时,我的应用程序就崩溃了。我什至没有到达 ViewController。我真的迷路了,如果还有更多让我知道的话,我会把必要的代码放在这里。谢谢!</p>
<p>错误 <code>argv char ** 0xbffffeda8 0xbffffeda8</code></p>
<p>数据模型:</p>
<p> <img src="/image/TNgTe.png" alt="enter image description here"/> </p>
<p>将项目添加到列表中的 .m:</p>
<pre><code>- (NSManagedObjectContext *)managedObjectContext {
NSManagedObjectContext *context = nil;
id delegate = [ delegate];
if () {
context = ;
}
return context;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = ;
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
;
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
;
// Dispose of any resources that can be recreated.
}
- (IBAction)cancel:(id)sender {
;
}
- (IBAction)save:(id)sender {
NSManagedObjectContext *context = ;
// Create a new managed object
NSManagedObject *newItem = ;
;
;
;
NSError *error = nil;
// Save the object to persistent store
if (!) {
NSLog(@"Can't Save! %@ %@", error, );
}
;
}
@end
</code></pre>
<p>.m 显示列表中的所有项目:</p>
<pre><code>- (NSManagedObjectContext *)managedObjectContext {
NSManagedObjectContext *context = nil;
id delegate = [ delegate];
if () {
context = ;
}
return context;
}
- (void)viewDidAppear:(BOOL)animated
{
;
// Fetch the lists from persistent data store
NSManagedObjectContext *managedObjectContext = ;
NSFetchRequest *fetchRequest = [ initWithEntityName:@"Item"];
self.lists = [ mutableCopy];
self.items = [ mutableCopy];
;
}
- (id)initWithStyle:(UITableViewStyle)style
{
self = ;
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
;
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)didReceiveMemoryWarning
{
;
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return self.items.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = ;
// Configure the cell...
NSManagedObject *item = ;
];
return cell;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSManagedObjectContext *context = ;
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete object from database
];
NSError *error = nil;
if (!) {
NSLog(@"Can't Delete! %@ %@", error, );
return;
}
// Remove device from table view
;
withRowAnimation:UITableViewRowAnimationFade];
}
}
#pragma mark - Segue
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([ isEqualToString:@"ShowDetails"]) {
NSManagedObject *selectedItem = row]];
AddViewController *destViewController = segue.destinationViewController;
destViewController.items = selectedItem;
}
}
@end
</code></pre>
<p>而 .m 显示所有列表:</p>
<pre><code>- (NSManagedObjectContext *)managedObjectContext {
NSManagedObjectContext *context = nil;
id delegate = [ delegate];
if () {
context = ;
}
return context;
}
- (void)viewDidAppear:(BOOL)animated
{
;
// Fetch the lists from persistent data store
NSManagedObjectContext *managedObjectContext = ;
NSFetchRequest *fetchRequest = [ initWithEntityName:@"List"];
self.lists = [ mutableCopy];
self.items = [ mutableCopy];
;
}
- (id)initWithStyle:(UITableViewStyle)style
{
self = ;
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
;
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)didReceiveMemoryWarning
{
;
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return self.lists.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = ;
// Configure the cell...
NSManagedObject *list = ;
];
return cell;
}
-(IBAction)add:(id)sender {
UIAlertView *alert = [ initWithTitle:@"Add List" message:@"Create a New Wish List" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Save", nil];
;
;
;
alert.delegate = self;
}
- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex != 0 && alertView.tag == 2) {
UITextField *tf = ;
NSManagedObjectContext *context = ;
// Create a new managed object
NSManagedObject *newList = ;
;
NSError *error = nil;
// Save the object to persistent store
if (!) {
NSLog(@"Can't Save! %@ %@", error, );
}
;
}
NSManagedObjectContext *managedObjectContext = ;
NSFetchRequest *fetchRequest = [ initWithEntityName:@"List"];
self.lists = [ mutableCopy];
self.items = [ mutableCopy];
;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSManagedObjectContext *context = ;
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete object from database
];
NSError *error = nil;
if (!) {
NSLog(@"Can't Delete! %@ %@", error, );
return;
}
// Remove list from table view
;
;
withRowAnimation:UITableViewRowAnimationFade];
}
}
@end
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>您应该考虑使用 <code>NSFetchedResultsController</code> 来让您的生活更轻松,让您的应用程序更高效。</p>
<p>这是不正确的:</p>
<pre><code>self.lists = [ mutableCopy];
self.items = [ mutableCopy];
</code></pre>
<p>因为这些集合应该包含不同类型的实体实例。但是,您实际上并不需要 <code>self.items</code>。该模型包含 <code>List</code> 和 <code>Item</code> 之间的关系,您应该在需要时使用该关系(或使用该关系的获取请求)获取指定列表的项目.并删除 <code>self.items</code>,这只会让你感到困惑。在 ListViewController 中,您仍然不需要这些项目。</p>
<p>当您按下以显示项目时,您应该获得选定的 <code>List</code> 并传递它。所以你有 <code>destViewController.items = selectedItem;</code> 你应该有类似 <code>destViewController.list = selectedList;</code> 的东西,然后项目 Controller 再次使用关系来显示适当的项目.</p>
<p>而且,当您添加一个新项目时,您必须将其与 <code>self.list</code> 关联:</p>
<pre><code>;
</code></pre>
<p>您还应该使用托管对象子类,管理它们的最佳方法是使用 <a href="https://github.com/rentzsch/mogenerator" rel="noreferrer noopener nofollow">mogenerator</a> .</p>
<p>旁白:如果你有 <code>if ([delegate performSelector:</code> 它应该是 <code>if ([delegate respondsToSelector:</code>,并且如果你没有得到上下文返回可能会记录...</p></p>
<p style="font-size: 20px;">关于ios - 推送查看时核心数据崩溃,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/22897934/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/22897934/
</a>
</p>
页:
[1]