2016-11-03 4 views
0

피트니스 트래커 앱에서 현재 실행 기록을 저장하고 실행이 발생한 위치 이름을 가져 오는 역 지오테이션 방법을 호출합니다.RealmSwift - 쓰기 시작/커밋

먼저 나는 쓰기 'latestRun'영역에, 그때 완료하고 수정하려고하는 역 지오 코딩을 기다리는 'latestRun'변수

try! realm.write { 
     realm.add(latestRun) 
    } 

if !savedLocations.isEmpty 
    { 
     ReverseGeocoder.getPlace(latitude: savedLocations[0].latitude, longitude: savedLocations[0].longitude, completionHandler: { 
      place in 
      realm.beginWrite() 
      try! realm.write { 
       latestRun.place = place 

      } 
      try! realm.commitWrite() 

     }) 
    } 

는 예외

Terminating app due to uncaught exception 'RLMException', reason: 'The Realm is already in a write transaction'

와 결과 무엇 내가 틀렸어? 내가 문서에서 발견 한 것은 beginWrite()commitWrite() 메쏘드가이 경우에 사용되어야한다는 것입니다. 그들은 둘 다 "기록 세션"을 열고, 같은 일을 단지

try! realm.write { 

전에

답변

1

에게 어느 commitWrite()beginWrite()

try! realm.write { 
    latestRun.place = place 
} 
를 사용하는 필요를 삭제할 것을 잊지 마세요 폐쇄 변형으로 이동하기로 결정한 경우

충분하다

2

이 때문에 라인

realm.beginWrite() 

의입니다. 당신은 하나만 유지해야합니다.

참고 : 당신은 beginWrite 라인을 삭제하고 또한 라인

try! realm.commitWrite() 
+0

지금 나는 치명적인 오류가있다 : '시도!' 표현식이 예기치 않게 오류를 발생 시켰습니다. Error Domain = io.realm Code = 1 "존재하지 않는 쓰기 트랜잭션을 커밋 할 수 없습니다." ' – DCDC

+0

'행을 남겨 뒀기 때문입니다! realm.commitWrite()'. 또한 삭제해야합니다. –