这很简单,但我似乎无法找到问题。新元素没有添加到数据库中,我的代码中没有出现任何错误。
这是添加方法
- (void) addPressed:(NSNotification *) message
{
// Insert a new record in the database
Track *track2 = [NSEntityDescription insertNewObjectForEntityForName: @"Track" inManagedObjectContext: managedObjectContext];
[managedObjectContext save: &error];
tracks = [self fetchTracks];
// Insert a new item in the table's data source
[tracks insertObject: track2 atIndex: 0];
// Insert a new row in the table
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection: 0];
addTrack.track = [tracks objectAtIndex: indexPath.row];
NSLog(@"here");
[[NSNotificationCenter defaultCenter] postNotificationName:@"choosePressed" object:self];
}
调用add后,它会向另一个视图控制器发送一条消息,用户从mpmediapicker中选择一首歌,然后点击一个选择此方法的保存按钮
- (void) finishedEditing
{
if(start.text.length == 0)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Warning" message: @"You must fill out all fields" delegate: self cancelButtonTitle: @"OK" otherButtonTitles: nil];
alert.tag = 1;
[alert show];
[alert release];
}
else if(duration.text.length == 0)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Warning" message: @"You must fill out all fields" delegate: self cancelButtonTitle: @"OK" otherButtonTitles: nil];
alert.tag = 1;
[alert show];
[alert release];
}
else if(name.text.length == 0)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Warning" message: @"You must fill out all fields" delegate: self cancelButtonTitle: @"OK" otherButtonTitles: nil];
alert.tag = 1;
[alert show];
[alert release];
}
else if(songLabel.text.length == 0)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Warning" message: @"You must select a song" delegate: self cancelButtonTitle: @"OK" otherButtonTitles: nil];
alert.tag = 1;
[alert show];
[alert release];
}
else
{
if (repeat)
{
track.title = NULL;
track.name = NULL;
repeat = NO;
}
else
{
songLabel.text = @"";
artistLabel.text = @"";
[self viewWillDisappear: YES];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
track.name = name.text;
track.duration = duration.text;
track.startloc = start.text;
track.title = [song valueForProperty:MPMediaItemPropertyTitle];
NSManagedObjectContext *context = track.managedObjectContext;
NSError *error = nil;
[context save: &error];
[[NSNotificationCenter defaultCenter] postNotificationName:@"myTraxsTab" object:self];
}
}
}
我使用NSFetchRequest而不是fetchedResults控制器。这是代码。
request = [[NSFetchRequest alloc] init];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey: @"name" ascending: YES];
sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
request.sortDescriptors = sortDescriptors;
[sortDescriptor release];
error = nil;
entity = [NSEntityDescription entityForName: @"Track" inManagedObjectContext: managedObjectContext];
request.entity = entity;
更新
好的,所以我在这里的else语句中添加了一行来打印数组
else
{
songLabel.text = @"";
artistLabel.text = @"";
[self viewWillDisappear: YES];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
track.name = name.text;
track.duration = duration.text;
track.startloc = start.text;
track.title = [song valueForProperty:MPMediaItemPropertyTitle];
NSManagedObjectContext *context = track.managedObjectContext;
NSError *error = nil;
[context save: &error];
NSLog(@"array: %@", tracks);
[[NSNotificationCenter defaultCenter] postNotificationName:@"myTraxsTab" object:self];
}
这就是它的印刷品。我不知道我失踪了什么。它让我发疯。我显然在名称,持续时间和开始标签上添加了值。
array: (
"<Track: 0x136d80> (entity: Track; id: 0x13a120 <x-coredata://9D789618-64C1-40BF-9F15-BA6A3EA715D3/Track/p7> ; data: {\n duration = nil;\n name = nil;\n startloc = nil;\n title = nil;\n})"
)
答案 0 :(得分:0)
如果你没有任何 crush 崩溃(不仅仅是在这种情况下),首先我会替换字符串:
[managedObjectContext save: &error];
有类似的东西:
// Save the managed object context.
NSError *error = nil;
if (![self.managedObjectContext save:&error]) {
NSLog(@"Failed to save object to managed object context: %@, %@", error, [error userInfo]);
}
也许(我认为最有可能),在这种情况下,NSlog会给你一些信息。 在任何情况下,男人,拯救是一个非常危险的操作,你不应该离开它没有额外的,至少是小的检查。
祝你好运! 修改强>
你能否发布实际如何从数据库中获取对象?据我了解,你在这里执行:
tracks = [self fetchTracks];
但是那里有什么? 另外,我觉得有点混乱:
我使用NSFetchRequest而不是fetchedResults控制器。
您创建一个NSFetchRequest对象,以便描述,您要从数据库中检索(获取)的内容。你做得很好,但我没有在代码中看到你实际使用这个请求然后...
出于检索的目的,正在使用NSFetchedResultsController 。
你把它设置为smth。像:
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc]
initWithFetchRequest:request
managedObjectContext:self.managedObjectContext
sectionNameKeyPath:nil cacheName:nil];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;
然后,为了执行实际的提取,你会做一些事情。像:
if (![self.fetchedResultsController performFetch:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
}
(之后你可以从fetchedResultsController获取所有对象)
OR ,如果您不想使用控制器,请直接获取数组:
NSError *error = nil;
NSArray *array = [managedObjectContext executeFetchRequest:request error:&error];
if (array == nil)
{
// Deal with error...
}