-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CoreData에서 Page 데이터 읽어오기 및 보여주기 (멀티미디어 포함 X) #101
Changes from 13 commits
05d3ee0
4c0322a
99dc370
bd5b741
78ef857
f03a713
4ae65ef
65aac5d
b04333b
d4b4d6c
f753762
b3d7d16
178ca5b
c5b6e0c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,24 +2,24 @@ import MHFoundation | |
import MHCore | ||
import CoreData | ||
|
||
final class CoreDataBookCoverStorage { | ||
public final class CoreDataBookCoverStorage { | ||
private let coreDataStorage: CoreDataStorage | ||
|
||
init(coreDataStorage: CoreDataStorage) { | ||
public init(coreDataStorage: CoreDataStorage) { | ||
self.coreDataStorage = coreDataStorage | ||
} | ||
} | ||
|
||
extension CoreDataBookCoverStorage: BookCoverStorage { | ||
func create(data: BookCoverDTO) async -> Result<Void, MHDataError> { | ||
public func create(data: BookCoverDTO) async -> Result<Void, MHDataError> { | ||
let context = coreDataStorage.persistentContainer.viewContext | ||
do { | ||
try await context.perform { | ||
guard let entity = NSEntityDescription.entity(forEntityName: "BookCoverEntity", in: context) else { | ||
throw MHDataError.noSuchEntity(key: "BookCoverEntity") | ||
} | ||
let bookCover = NSManagedObject(entity: entity, insertInto: context) | ||
bookCover.setValue(data.identifier, forKey: "identifier") | ||
bookCover.setValue(data.id, forKey: "id") | ||
bookCover.setValue(data.title, forKey: "title") | ||
bookCover.setValue(data.category, forKey: "category") | ||
bookCover.setValue(data.color, forKey: "color") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 코어데이터 CRUD 에 대한 메소드가 do-catch부터 entity 꺼내는 로직 등등 겹치는게 많아서 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 모두 수정했습니다 .ᐟ.ᐟ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 역시.. 말 안 해도 알아서 해주는 빛영현 |
||
|
@@ -38,7 +38,7 @@ extension CoreDataBookCoverStorage: BookCoverStorage { | |
} | ||
} | ||
|
||
func fetch() async -> Result<[BookCoverDTO], MHDataError> { | ||
public func fetch() async -> Result<[BookCoverDTO], MHDataError> { | ||
let context = coreDataStorage.persistentContainer.viewContext | ||
do { | ||
var bookCoverEntities: [BookCoverEntity] = [] | ||
|
@@ -57,14 +57,14 @@ extension CoreDataBookCoverStorage: BookCoverStorage { | |
} | ||
} | ||
|
||
func update(with id: UUID, data: BookCoverDTO) async -> Result<Void, MHDataError> { | ||
public func update(with id: UUID, data: BookCoverDTO) async -> Result<Void, MHDataError> { | ||
let context = coreDataStorage.persistentContainer.viewContext | ||
do { | ||
try await context.perform { [weak self] in | ||
guard let newEntity = try self?.getEntityByIdentifier(in: context, with: id) else { | ||
throw MHDataError.findEntityFailure | ||
} | ||
newEntity.setValue(data.identifier, forKey: "identifier") | ||
newEntity.setValue(data.id, forKey: "id") | ||
newEntity.setValue(data.title, forKey: "title") | ||
newEntity.setValue(data.category, forKey: "category") | ||
newEntity.setValue(data.color, forKey: "color") | ||
|
@@ -83,7 +83,7 @@ extension CoreDataBookCoverStorage: BookCoverStorage { | |
} | ||
} | ||
|
||
func delete(with id: UUID) async -> Result<Void, MHDataError> { | ||
public func delete(with id: UUID) async -> Result<Void, MHDataError> { | ||
let context = coreDataStorage.persistentContainer.viewContext | ||
do { | ||
try await context.perform { [weak self] in | ||
|
@@ -108,20 +108,20 @@ extension CoreDataBookCoverStorage: BookCoverStorage { | |
private func getEntityByIdentifier(in context: NSManagedObjectContext, with id: UUID) throws -> BookCoverEntity? { | ||
let request = BookCoverEntity.fetchRequest() | ||
|
||
return try context.fetch(request).first(where: { $0.identifier == id }) | ||
return try context.fetch(request).first(where: { $0.id == id }) | ||
} | ||
} | ||
|
||
// MARK: - Mapper | ||
extension CoreDataBookCoverStorage { | ||
// MARK: - CoreToDTO | ||
func coreBookCoverToDTO(_ bookCover: BookCoverEntity) -> BookCoverDTO? { | ||
guard let identifier = bookCover.identifier, | ||
guard let id = bookCover.id, | ||
let title = bookCover.title, | ||
let color = bookCover.color else { return nil } | ||
|
||
return BookCoverDTO( | ||
identifier: identifier, | ||
id: id, | ||
title: title, | ||
imageURL: bookCover.imageURL, | ||
color: color, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Identifiable을 안쓰는데 id로 바꾸신 이유가 있나용 ??
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
다른 곳 들과의 통일성을 위해서 바꿨습니다 ! Entity와 DTO, CoreDataModel 각각의 attributes 이름이 같아야 헷갈리지 않을 것이라고 생각하여 바꿔줬습니다!