Skip to content
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

[220824] TIL #112

Closed
Taehyeon-Kim opened this issue Aug 24, 2022 · 0 comments
Closed

[220824] TIL #112

Taehyeon-Kim opened this issue Aug 24, 2022 · 0 comments
Assignees

Comments

@Taehyeon-Kim
Copy link
Owner

Taehyeon-Kim commented Aug 24, 2022

Document Directory

각각의 앱은 모두 Sandbox화 되어 있다. 각자 고유의 바운더리를 가지고 있다는 뜻이다. 바운더리는 Container들로 이루어져 있는데 크게 Bundle Container, Data Container, iCloud Container로 이루어져 있다. 그 중에서 Documents 디렉터리는 Data Container 안에 위치해 있다.

Documents 디렉터리는 삭제, 변경되어도 무방하고 사용자가 다루는 컨텐츠와 관련된 파일들만 저장한다. Realm은 기본적으로 Document 경로를 사용한다.

FileManager

위에서 각자 고유한 공간을 가지고 있다고 했는데, 그 고유한 공간에 접근하기 위해 FileManager라는 클래스를 이용할 수 있다. 아래는 Document 디렉터리 아래에 이미지를 저장하고 제거하는 코드 예시이다.

/// 이미지 제거
func removeImageFromDocument(fileName: String) {
    guard let documentDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else { return }
    let fileURL = documentDirectory.appendingPathComponent(fileName)
    
    do {
        try FileManager.default.removeItem(at: fileURL)
        
    } catch let error {
        Logger.log(error, .error, "error occured")
    }
}

/// 이미지 저장
public func saveImageToDocument(fileName: String, image: UIImage) {
    guard let documentDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else { return }
    let fileURL = documentDirectory.appendingPathComponent(fileName)
    guard let data = image.jpegData(compressionQuality: 0.5) else { return }
    
    do {
        try data.write(to: fileURL)
    } catch let error {
        print("file save err", error)
    }
}

백업, 복구

기본적으로 프라이빗한 정보이거나, 데이터의 사이즈가 크지 않은 경우에는 로컬로 데이터를 관리하면 좋은 것 같다. 그 외에 특징을 살펴보면

  • 사용자간 데이터 공유 불필요
  • 별도 서버에 비용을 지급하면서 데이터를 유지할 필요가 없는 경우
  • 사용자가 필요로 할 때 데이터를 외부로 백업하도록 하고 싶은 경우

데이터는 앱이 삭제되는 순간, 모두 제거되기 때문에 백업, 복구가 필요하다.

외부 저장소

  1. iCloud
  2. Google Drive, Dropbox 등의 써드 파티 클라우드 서비스
  3. 파일 앱(기본 앱) + ShareActivity와 함께 사용하는 경우 큰 장점이 있음

데이터 전달 (Protocol/Delegate)

프로토콜을 이용해서 데이터 전달을 할 수 있다. 생각보다 자주 사용했던 것 같다.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant