Skip to content

Commit

Permalink
feat: fix remove character
Browse files Browse the repository at this point in the history
  • Loading branch information
xramos committed Apr 8, 2024
1 parent d9078eb commit 2c7e144
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 45 deletions.
58 changes: 14 additions & 44 deletions MyComics/MyComics/Database/DBManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ extension DBManager: Persistence {

let dbCharacter = DBCharacter(context: coreDataStack.managedContext)
dbCharacter.id = Int32(character.id)
dbCharacter.characterId = Int32(character.id)
dbCharacter.name = character.name
dbCharacter.realName = character.realName
dbCharacter.aliases = character.aliases
Expand All @@ -62,7 +63,8 @@ extension DBManager: Persistence {
let characterId = Int32(character.id)

let fetchRequest = NSFetchRequest<DBCharacter>(entityName: "DBCharacter")
fetchRequest.predicate = NSPredicate(format: "id==\(characterId)")
fetchRequest.predicate = NSPredicate(format: "characterId==\(characterId)")

do {

let dbCharacters = try coreDataStack.managedContext.fetch(fetchRequest)
Expand All @@ -79,25 +81,21 @@ extension DBManager: Persistence {
}
}

func existCharacter(character: Character) -> Bool {
func getCharacter(id: Int) -> Character? {

let characterId = Int32(character.id)
let characters = getCharacters()

let fetchRequest = NSFetchRequest<DBCharacter>(entityName: "DBCharacter")
fetchRequest.predicate = NSPredicate(format: "id==\(characterId)")

do {

let dbCharacters = try coreDataStack.managedContext.fetch(fetchRequest)
for character in characters where character.id == id {

return dbCharacters.first != nil

} catch let error as NSError {

print("Could not fetch for delete. \(error), \(error.userInfo)")

return false
return character
}

return nil
}

func existCharacter(character: Character) -> Bool {

return getCharacter(id: character.id) != nil
}

func getCharacters() -> [Character] {
Expand All @@ -122,32 +120,4 @@ extension DBManager: Persistence {

return characters
}

func getCharacter(id: Int) -> Character? {

let characterId = Int32(id)

let fetchRequest = NSFetchRequest<DBCharacter>(entityName: "DBCharacter")
fetchRequest.predicate = NSPredicate(format: "id==\(characterId)")

do {

let dbCharacters = try coreDataStack.managedContext.fetch(fetchRequest)

if let dbCharacter = dbCharacters.first {

return dbCharacter.convertToEntity()

} else {

return nil
}

} catch let error as NSError {

print("Could not fetch character id. \(error), \(error.userInfo)")

return nil
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<entity name="DBCharacter" representedClassName="DBCharacter" syncable="YES" codeGenerationType="class">
<attribute name="aliases" optional="YES" attributeType="String"/>
<attribute name="birth" optional="YES" attributeType="String"/>
<attribute name="characterId" optional="YES" attributeType="Integer 32" defaultValueString="0" usesScalarValueType="YES"/>
<attribute name="deck" optional="YES" attributeType="String"/>
<attribute name="gender" optional="YES" attributeType="Integer 16" defaultValueString="0" usesScalarValueType="YES"/>
<attribute name="id" transient="YES" attributeType="Integer 32" defaultValueString="0" usesScalarValueType="YES"/>
Expand Down
2 changes: 1 addition & 1 deletion MyComics/MyComics/Entitites/DBEntities/DBCharacter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ extension DBCharacter {
pows = dbPowers.map({ $0.convertToEntity()})
}

return Character(id: Int(id),
return Character(id: Int(characterId),
name: name ?? "",
realName: realName ?? "",
aliases: aliases ?? "",
Expand Down

0 comments on commit 2c7e144

Please sign in to comment.