我正在尝试使用 CLLocation Manager 来获取某人的位置,每当他们移动并将 lat、lng 和时间戳存储到核心数据中,然后将其显示在表视图选项卡中。但是,控制台中的输出始终通过抛出此日志指示 managedObjectContext 为空coredataproject[12478:11903] After managedObjectContext: <NSManagedObjectContext: 0x7248230>
这是我的 AppDelgate 实现文件中的相关代码
#import "AppDelegate.h"
#import "RootViewController.h"
#import "FirstViewController.h"
@implementation AppDelegate
@synthesize window;
@synthesize navigationController;
#pragma mark -
#pragma mark Application lifecycle
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Configure and show the window.
RootViewController *rootViewController = [[RootViewController alloc] initWithStyle:UITableViewStylePlain];
NSManagedObjectContext *context = [self managedObjectContext];
if (!context) {
NSLog(@"Could not create context for self");
}
rootViewController.managedObjectContext = context;
UINavigationController *aNavigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
self.navigationController = aNavigationController;
[window addSubview:[navigationController view]];
[window makeKeyAndVisible];
}
/**
applicationWillTerminate: saves changes in the application's managed object context before the application terminates.
*/
- (void)applicationWillTerminate:(UIApplication *)application {
NSError *error;
if (managedObjectContext != nil) {
if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) {
// Handle the error.
}
}
}
这是 FirstViewController.M 代码,我在其中获取位置并将其存储在核心数据“事件”实体中
- (void)viewDidLoad
{
locationManager =[[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = kCLDistanceFilterNone;
[locationManager startUpdatingLocation];
-(void) locationmanager: (CLLocationManager *) manager
didUpdateToLocation: (CLLocation *) newLocation
fromLocation: (CLLocation *) oldLocation
{
CLLocation *location = [locationManager location];
if (!location) {
return;
}
/*
Create a new instance of the Event entity.
*/
RootViewController *rootviewcontroller = [RootViewController alloc];
Event *event = (Event *)[NSEntityDescription insertNewObjectForEntityForName:@"Event" inManagedObjectContext:rootviewcontroller.managedObjectContext];
// Configure the new event with information from the location.
CLLocationCoordinate2D coordinate = [location coordinate];
[event setLatitude:[NSNumber numberWithDouble:coordinate.latitude]];
[event setLongitude:[NSNumber numberWithDouble:coordinate.longitude]];
// Should be the location's timestamp, but this will be constant for simulator.
// [event setCreationDate:[location timestamp]];
[event setTimeStamp:[NSDate date]];
// Commit the change.
NSError *error;
if (![rootviewcontroller.managedObjectContext save:&error]) {
NSLog(@"Save Error");
}
//RootViewController *rootviewcontroller = [RootViewController alloc];
[rootviewcontroller.eventsArray insertObject:event atIndex:0];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[rootviewcontroller.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[rootviewcontroller.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
最后是 RootViewController 文件,我试图在其中获取和显示核心数据中的内容。当我单击此选项卡时,控制台告诉我 managedObjectConsole 为 nill
- (void)viewDidLoad {
[super viewDidLoad];
if (managedObjectContext == nil)
{
managedObjectContext = [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
NSLog(@"After managedObjectContext: %@", managedObjectContext);
}
// Set the title.
self.title = @"Locations";
/*
Fetch existing events.
Create a fetch request; find the Event entity and assign it to the request; add a sort descriptor; then execute the fetch.
*/
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Event" inManagedObjectContext:managedObjectContext];
[request setEntity:entity];
// Order the events by time stamp, most recent first.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"timeStamp" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];
// Execute the fetch -- create a mutable copy of the result.
NSError *error = nil;
NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
if (mutableFetchResults == nil) {
NSLog(@"Mutable Fetch Results equals nill");
}
// Set self's events array to the mutable array, then clean up.
[self setEventsArray:mutableFetchResults];
}
一旦表格数据存在,我也会做更多的事情来组织表格数据,但我认为这不是问题所在。
我不确定为什么 managedObjectContext 中没有任何内容,因为它应该具有来自位置管理器的位置数据。我对核心数据不太熟悉,所以我可能只是做一些简单的错误,但任何见解都将不胜感激!