gpt4 book ai didi

ios - nil中的managedObjectModel(仅在WatchApp中)

转载 作者:行者123 更新时间:2023-12-01 18:12:31 25 4
gpt4 key购买 nike

我正在更新Apple Watch的应用程序
这个应用程序使用coredata,所以我创建了一个框架来管理Core Data堆栈!
当我在设备或模拟器上运行该应用程序时,该应用程序运行良好,但是当我在Apple Watch模拟器上运行该应用程序时,此日志崩溃

***由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“无法创建
带有nil模型的NSPersistentStoreCoordinator

问题似乎是managedObjectModel,如果我将其记录下来

NSLog(@"managedObjectModel %@", _managedObjectModel);

日志返回

managedObjectModel(空)

框架中的代码似乎正确,实际上主应用程序完美运行

无论如何,这就是框架的内容
#import "DataAccess.h"

@implementation DataAccess

@synthesize managedObjectContext = _managedObjectContext;
@synthesize managedObjectModel = _managedObjectModel;
@synthesize persistentStoreCoordinator = _persistentStoreCoordinator;


+ (instancetype)sharedInstance
{
static DataAccess *sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [[DataAccess alloc] init];
// Do any other initialisation stuff here
});
return sharedInstance;
}

- (void)saveContext
{
NSError *error = nil;
NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
if (managedObjectContext != nil) {
if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}
}



#pragma mark - Core Data stack

// Returns the managed object context for the application.
// If the context doesn't already exist, it is created and bound to the persistent store coordinator for the application.
- (NSManagedObjectContext *)managedObjectContext
{
if (_managedObjectContext != nil) {
return _managedObjectContext;
}

NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil) {
_managedObjectContext = [[NSManagedObjectContext alloc] init];
[_managedObjectContext setPersistentStoreCoordinator:coordinator];
}
return _managedObjectContext;
}

// Returns the managed object model for the application.
// If the model doesn't already exist, it is created from the application's model.
- (NSManagedObjectModel *)managedObjectModel
{
if (_managedObjectModel != nil) {

NSLog(@"managedObjectModel %@", _managedObjectModel);

return _managedObjectModel;

}
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"MyApp" withExtension:@"momd"];
_managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];

NSLog(@"managedObjectModel %@", _managedObjectModel);


return _managedObjectModel;
}

// Returns the persistent store coordinator for the application.
// If the coordinator doesn't already exist, it is created and the application's store added to it.
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (_persistentStoreCoordinator != nil) {
return _persistentStoreCoordinator;
}

//NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"TMM.sqlite"];
//NSURL *storeURL = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSAllDomainsMask] lastObject];
// storeURL = [storeURL URLByAppendingPathComponent:@"db.sqlite"];

NSURL *storeURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.ragazzetto.MyApp.Documents"];
storeURL = [storeURL URLByAppendingPathComponent:@"MyApp.sqlite"];

NSError *error = nil;
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];

if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {

NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}

return _persistentStoreCoordinator;
}
#pragma mark - Application's Documents directory

// Returns the URL to the application's Documents directory.
- (NSURL *)applicationDocumentsDirectory
{
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}


@end

问题出在哪里 ?

感谢您的帮助

最佳答案

当您编写iOS应用扩展程序(包括所有当前的WatchKit应用程序)时,您将使用其自己的捆绑包创建一个单独的可执行文件。应用程序中的资源不一定在扩展程序中可用,反之亦然。因此,当您执行此操作时:

NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"MyApp" withExtension:@"momd"];
_managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];

在WatchKit应用程序中运行时,URL与包含应用程序中的URL不同。该错误告诉您WatchKit应用程序的URL无效,即WatchKit应用程序包中没有模型文件。

简单的解决方法是仅将模型包含在WatchKit捆绑包中。为此,请执行以下操作:
  • 在Xcode中选择模型文件
  • 打开Xcode窗口右侧的文件检查器面板
  • 在“目标成员资格”部分中查找。确保选择了WatchKit目标。

  • 那应该可以,但是将意味着您有两个模型文件副本。更好的方法是将模型文件放入两个目标都使用的共享框架中。稍微复杂一点,但是稍作搜索就会找到详细的步骤。

    关于ios - nil中的managedObjectModel(仅在WatchApp中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27784069/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com