核心数据中具有 nil 属性的额外实体

发布于 2025-01-19 22:33:24 字数 1877 浏览 1 评论 0原文

这是我的代码。我有核心数据中具有零属性的额外实体。当我首先删除和运行应用程序时,我会从核心数据中获取一个零属性的保存对象。

class RepositoryEntity {

private var context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext

func fetchRepositories() -> [RepositoryEntity] {
    do {
        return try context.fetch(RepositoryEntity.fetchRequest())
    } catch(let error) {
        print("errr: ", error.localizedDescription)
    }
    return []
}

func saveObject(repo: Repository, onSuccess: () -> Void, onFailure: (_ error: String) -> Void) {
        let repoEntity = RepositoryEntity(context: self.context)
        repoEntity.fullName = repo.fullName
        repoEntity.dateCreated = repo.dateCreated
        repoEntity.url = repo.url
        repoEntity.language = repo.language
        repoEntity.repoDescription = repo.repoDescription
        repoEntity.id = repo.id

        let ownerEntity = OwnerEntity(context: self.context)
        ownerEntity.ownerName = repo.owner.ownerName
        ownerEntity.avatarUrl = repo.owner.avatarUrl

        repoEntity.addToOwner(ownerEntity)
        
//        Save the data
        do {
            try context.save()
            onSuccess()
        } catch(let error) {
            onFailure("Something Happend. Try again later.")
            print(error.localizedDescription)
    }
}

func deleteRepository(repo: Repository, onSuccess: () -> Void, onFailure: (_ error: String) -> Void) {
   
    let repositories = fetchRepositories()
    guard let deletableRepo = repositories.first(where: {$0.id == repo.id}) else { return }

    self.context.delete(deletableRepo)
    
    do {
        try context.save()
        onSuccess()
    } catch(let error) {
        onFailure("Something Happens. try again later.")
        print(error.localizedDescription)
    }
    
}

}

当我首先删除并运行应用程序时,我会从核心数据中获取一个零属性的保存对象。

Here is my code. I have extra entities with nil attributes in Core Data. when I delete and run application firstly, I get one saved object with nil attributes fetched from core data.

class RepositoryEntity {

private var context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext

func fetchRepositories() -> [RepositoryEntity] {
    do {
        return try context.fetch(RepositoryEntity.fetchRequest())
    } catch(let error) {
        print("errr: ", error.localizedDescription)
    }
    return []
}

func saveObject(repo: Repository, onSuccess: () -> Void, onFailure: (_ error: String) -> Void) {
        let repoEntity = RepositoryEntity(context: self.context)
        repoEntity.fullName = repo.fullName
        repoEntity.dateCreated = repo.dateCreated
        repoEntity.url = repo.url
        repoEntity.language = repo.language
        repoEntity.repoDescription = repo.repoDescription
        repoEntity.id = repo.id

        let ownerEntity = OwnerEntity(context: self.context)
        ownerEntity.ownerName = repo.owner.ownerName
        ownerEntity.avatarUrl = repo.owner.avatarUrl

        repoEntity.addToOwner(ownerEntity)
        
//        Save the data
        do {
            try context.save()
            onSuccess()
        } catch(let error) {
            onFailure("Something Happend. Try again later.")
            print(error.localizedDescription)
    }
}

func deleteRepository(repo: Repository, onSuccess: () -> Void, onFailure: (_ error: String) -> Void) {
   
    let repositories = fetchRepositories()
    guard let deletableRepo = repositories.first(where: {$0.id == repo.id}) else { return }

    self.context.delete(deletableRepo)
    
    do {
        try context.save()
        onSuccess()
    } catch(let error) {
        onFailure("Something Happens. try again later.")
        print(error.localizedDescription)
    }
    
}

}

when I delete and run application firstly, I get one saved object with nil attributes fetched from core data.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心舞飞扬 2025-01-26 22:33:24

当你写“我得到一个保存的对象...”时:什么对象? RepositoryEntity?通过调用fetchRepositories(),您如何知道您已经保存了对象?我只能假设它是这样的(而不是有一个空的OwnerEntity)。

在这种情况下,问题在于,要调用 func fetchRepositories() ,您需要创建一个实例。因此,当您从零个对象开始时,只要您调用 fetchRepositories(),您就已经拥有至少一个对象。

更改:

func fetchRepositories() -> [RepositoryEntity]

with:

static func fetchRepositories() -> [RepositoryEntity]

并从类型调用它:

RepositoryEntity.fetchRepositories()

对于 deleteRepository 也是如此。

When you write "I get one saved object...": what object? RepositoryEntity? How do you know you have a saved object, by calling fetchRepositories()? I can only assume it's like that (as opposed to having an empty OwnerEntity).

In that case, the problem is that, to call func fetchRepositories() you need to create an instance. So, when you start with zero objects, as soon as you call fetchRepositories() you already have at least one.

Change:

func fetchRepositories() -> [RepositoryEntity]

with:

static func fetchRepositories() -> [RepositoryEntity]

and call it from the type:

RepositoryEntity.fetchRepositories()

The same also for deleteRepository.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文