我可以通过编码在核心数据中创建和删除实体

时间:2021-09-01 06:37:14

I want to create and delete entity in core data by coding(no gui-Swift3), is it possible?

我想通过编码(没有gui-Swift3)在核心数据中创建和删除实体,是否可能?

4 个解决方案

#1


1  

You can create entities at run time as well as instances, although it's very unusual and has a couple of potential problems to be aware of.

您可以在运行时创建实体以及实例,尽管它非常不寻常并且有一些潜在的问题需要注意。

First, you can create instances of NSEntityDescription to create a new entity. Use instances of NSAttributeDescription and NSRelationshipDescription to complete the new entity. Add the new entity to a managed object model by modifying the entities property on your NSManagedObjectModel.

首先,您可以创建NSEntityDescription的实例以创建新实体。使用NSAttributeDescription和NSRelationshipDescription的实例来完成新实体。通过修改NSManagedObjectModel上的entities属性,将新实体添加到托管对象模型。

Things you need to know before attempting this:

在尝试此操作之前您需要了解的事项:

  1. The model can't be modified after you have loaded a persistent store file. So you must do the above before attempting to access any data with the model. Models are read/write until you load data but read only afterward. Modifying the model after loading data will cause your app to crash.
  2. 加载持久性存储文件后,无法修改模型。因此,在尝试使用模型访问任何数据之前,必须执行上述操作。模型是读/写,直到您加载数据,但之后只读。加载数据后修改模型将导致您的应用崩溃。
  3. You must create the same entity every time you use it with a persistent store file-- unless you perform model migration to reflect the new entity description.
  4. 每次将其与持久性存储文件一起使用时,必须创建相同的实体 - 除非您执行模型迁移以反映新的实体描述。
  5. You can't use NSPersistentContainer, because it hides too many of the details. You'll have to use the older (but still supported) approach where you write your own code to load the model and then use addPersistentStore(ofType:configurationName:at:options:) to load your persistent store file.
  6. 您不能使用NSPersistentContainer,因为它隐藏了太多细节。您必须使用较旧(但仍受支持)的方法,您可以编写自己的代码来加载模型,然后使用addPersistentStore(ofType:configurationName:at:options :)来加载持久性存储文件。

#2


0  

You can create and delete records of an entity, but not the entity itself.

您可以创建和删除实体的记录,但不能创建实体本身。

The model cannot be changed at runtime.

无法在运行时更改模型。

#3


0  

I think you can't create/delete entity because model will not be changed run time..You can Create / Update / Delete / Select records from entity.

我认为您无法创建/删除实体,因为模型不会在运行时更改。您可以创建/更新/删除/从实体中选择记录。

#4


0  

Three functions to save or delete objects in coredata:

保存或删除coredata中对象的三个函数:

-(BOOL)deleteObjectFromDB:(NSManagedObject *)object eSalva:(BOOL)andSave {

- (BOOL)deleteObjectFromDB:(NSManagedObject *)object eSalva:(BOOL)andSave {

if (!object) {

    NSLog(@"DB Error");
    return NO;

}

[_managedObjectContext deleteObject:object];

if (andSave)
    return [self saveDB];

return YES;

}

}

-(BOOL)saveDB {

- (BOOL)saveDB {

BOOL result;
result = [_managedObjectContext save:nil];

if (!result)
    NSLog(@"DB Error: database saving error");

return result;

}

}

deleting all objects

删除所有对象

-(void) deleteAllObjects: (NSString *) entityDescription {

- (void)deleteAllObjects:(NSString *)entityDescription {

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:entityDescription inManagedObjectContext:_managedObjectContext];

[fetchRequest setEntity:entity];

NSError *error;
NSArray *items = [_managedObjectContext executeFetchRequest:fetchRequest error:&error];
[fetchRequest release];


for (NSManagedObject *managedObject in items) {
    [_managedObjectContext deleteObject:managedObject];
    DLog(@"%@ object deleted",entityDescription);
}
if (![_managedObjectContext save:&error]) {
    DLog(@"Error deleting %@ - error:%@",entityDescription,error);
}

}

}

Returning to your question:

回到你的问题:

here there is a way to delete entities

这里有一种删除实体的方法

hope it helps.

希望能帮助到你。

#1


1  

You can create entities at run time as well as instances, although it's very unusual and has a couple of potential problems to be aware of.

您可以在运行时创建实体以及实例,尽管它非常不寻常并且有一些潜在的问题需要注意。

First, you can create instances of NSEntityDescription to create a new entity. Use instances of NSAttributeDescription and NSRelationshipDescription to complete the new entity. Add the new entity to a managed object model by modifying the entities property on your NSManagedObjectModel.

首先,您可以创建NSEntityDescription的实例以创建新实体。使用NSAttributeDescription和NSRelationshipDescription的实例来完成新实体。通过修改NSManagedObjectModel上的entities属性,将新实体添加到托管对象模型。

Things you need to know before attempting this:

在尝试此操作之前您需要了解的事项:

  1. The model can't be modified after you have loaded a persistent store file. So you must do the above before attempting to access any data with the model. Models are read/write until you load data but read only afterward. Modifying the model after loading data will cause your app to crash.
  2. 加载持久性存储文件后,无法修改模型。因此,在尝试使用模型访问任何数据之前,必须执行上述操作。模型是读/写,直到您加载数据,但之后只读。加载数据后修改模型将导致您的应用崩溃。
  3. You must create the same entity every time you use it with a persistent store file-- unless you perform model migration to reflect the new entity description.
  4. 每次将其与持久性存储文件一起使用时,必须创建相同的实体 - 除非您执行模型迁移以反映新的实体描述。
  5. You can't use NSPersistentContainer, because it hides too many of the details. You'll have to use the older (but still supported) approach where you write your own code to load the model and then use addPersistentStore(ofType:configurationName:at:options:) to load your persistent store file.
  6. 您不能使用NSPersistentContainer,因为它隐藏了太多细节。您必须使用较旧(但仍受支持)的方法,您可以编写自己的代码来加载模型,然后使用addPersistentStore(ofType:configurationName:at:options :)来加载持久性存储文件。

#2


0  

You can create and delete records of an entity, but not the entity itself.

您可以创建和删除实体的记录,但不能创建实体本身。

The model cannot be changed at runtime.

无法在运行时更改模型。

#3


0  

I think you can't create/delete entity because model will not be changed run time..You can Create / Update / Delete / Select records from entity.

我认为您无法创建/删除实体,因为模型不会在运行时更改。您可以创建/更新/删除/从实体中选择记录。

#4


0  

Three functions to save or delete objects in coredata:

保存或删除coredata中对象的三个函数:

-(BOOL)deleteObjectFromDB:(NSManagedObject *)object eSalva:(BOOL)andSave {

- (BOOL)deleteObjectFromDB:(NSManagedObject *)object eSalva:(BOOL)andSave {

if (!object) {

    NSLog(@"DB Error");
    return NO;

}

[_managedObjectContext deleteObject:object];

if (andSave)
    return [self saveDB];

return YES;

}

}

-(BOOL)saveDB {

- (BOOL)saveDB {

BOOL result;
result = [_managedObjectContext save:nil];

if (!result)
    NSLog(@"DB Error: database saving error");

return result;

}

}

deleting all objects

删除所有对象

-(void) deleteAllObjects: (NSString *) entityDescription {

- (void)deleteAllObjects:(NSString *)entityDescription {

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:entityDescription inManagedObjectContext:_managedObjectContext];

[fetchRequest setEntity:entity];

NSError *error;
NSArray *items = [_managedObjectContext executeFetchRequest:fetchRequest error:&error];
[fetchRequest release];


for (NSManagedObject *managedObject in items) {
    [_managedObjectContext deleteObject:managedObject];
    DLog(@"%@ object deleted",entityDescription);
}
if (![_managedObjectContext save:&error]) {
    DLog(@"Error deleting %@ - error:%@",entityDescription,error);
}

}

}

Returning to your question:

回到你的问题:

here there is a way to delete entities

这里有一种删除实体的方法

hope it helps.

希望能帮助到你。