Refactor iOS sync with Database class and add error test cases