[iOS] core data stack

Zoe·2022년 11월 2일
0

iOS

목록 보기
11/39

core data stack


manage and persist your app's model layer
영구적으로 모델을 저장한 계층을 관리

  • context로만 db를 사용할 수 있다
  • persistent container = model + context + store coordinator
  • core data programming guide
  • persistent store coordinator 를 통해서 경로 접근
  • ios10 이후로는 context만 있어도 됨

✅ core data 구조 생성 local db구조

  • entity = table
  • 구분자 UUID -> identifier
let appdelegate = UIApplication.shared.delegate as! Appdelegate // 싱글톤으로 가져옴
appdelegate.persistentContainer.viewContext

func fetchData() {
	let fetchRequest: NSFetchRequest<TodoList> = TodoList.fetchRequest()

	let context = appdelegate.persistentContainer.viewContext
	
    do {
    	self.todoList = try context.fetch(fetchRequest)
	} catch {
    	print(error)
    }
}
//table view
let cell = tableView.dequeReusableCell(withIdentifier: "TodoCell", for: indexPath) as! TodoCell

cell.topTitleLabel.text = todoList[indexPath.row].title

if let hasDate = todoList[indexPath.row].date {
	let formatter = DateFormatter()
    formatter.dateFormat = "MM-dd hh:mm:ss"
    let dateString = formatter.string(from: hasDate)
    cell.dateLabel.text = dateString
} else {
	cell.dateLabel.text = ""
}

return cell
fetchData()
todoTableView.reloadData()
profile
iOS 개발자😺

0개의 댓글