|
1943 | 1943 | return |
1944 | 1944 | } |
1945 | 1945 |
|
1946 | | - serverRecord.userModificationTime = metadata.userModificationTime |
| 1946 | + let ancestorRecord = metadata._lastKnownServerRecordAllFields |
| 1947 | + |
| 1948 | + let hasServerChanged: Bool = { |
| 1949 | + guard |
| 1950 | + let ancestorRecord, |
| 1951 | + let ancestorChangeTag = ancestorRecord.recordChangeTag, |
| 1952 | + let serverChangeTag = serverRecord.recordChangeTag |
| 1953 | + else { |
| 1954 | + // Without an ancestor, the server record is upserted as-is. In the unlikely edge |
| 1955 | + // case where a matching client row exists, it gets overwritten. |
| 1956 | + return true |
| 1957 | + } |
| 1958 | + return ancestorChangeTag != serverChangeTag |
| 1959 | + }() |
| 1960 | + |
| 1961 | + let hasClientChanged: Bool = { |
| 1962 | + // Without an ancestor, we can't detect client changes (no baseline to compare |
| 1963 | + // against). This effectively falls through to server wins. |
| 1964 | + guard let ancestorRecord else { return false } |
| 1965 | + return metadata.userModificationTime > ancestorRecord.userModificationTime |
| 1966 | + }() |
| 1967 | + |
| 1968 | + let hasConflict = hasServerChanged && hasClientChanged |
| 1969 | + print("hasServerChanged", hasServerChanged, "hasClientChanged", hasClientChanged, "hasConflict", hasConflict) |
| 1970 | + |
| 1971 | + if hasConflict { |
| 1972 | + // Sets the record-level userModificationTime to the max of the client and server |
| 1973 | + // modification times, which effectively records the time at which the conflict |
| 1974 | + // resolution has happened. The resolved record is then stored as the new last-known |
| 1975 | + // server record, ensuring that per-field timestamps on the next upload reflect |
| 1976 | + // the resolution time rather than the server's original timestamps. |
| 1977 | + serverRecord.userModificationTime = metadata.userModificationTime |
| 1978 | + } |
1947 | 1979 |
|
1948 | 1980 | func open<T>(_ table: some SynchronizableTable<T>) throws { |
1949 | 1981 | var columnNamesToUpsert = Set(T.TableColumns.writableColumns.map(\.name)) |
|
0 commit comments