diff --git a/FreeTubeSyncer/REST/Syncer.cs b/FreeTubeSyncer/REST/Syncer.cs index c984257..3aee8b0 100644 --- a/FreeTubeSyncer/REST/Syncer.cs +++ b/FreeTubeSyncer/REST/Syncer.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.IO; using System.Linq; +using System.Text; using System.Text.Json; using System.Text.Json.Nodes; using System.Threading.Tasks; @@ -40,7 +41,6 @@ public class Syncer : ISyncer where T : class, IDataModel, new() _dbName = dbName; _restEndpoint = restEndpoint; FetchDatabase().Wait(); - ReadDatabase().Wait(); } public bool IsDirty() => _isDirty; @@ -84,11 +84,11 @@ public class Syncer : ISyncer where T : class, IDataModel, new() if (data.Equals(entry)) continue; - Console.WriteLine($"Updated Entry for {_dbName}"); + Console.WriteLine($"Updated Entry for {_dbName} - {entry.Id()}"); _entries.RemoveAll(x => x.EqualId(entry.Id())); } else - Console.WriteLine($"New Entry for {_dbName}"); + Console.WriteLine($"New Entry for {_dbName} - {entry.Id()}"); _entries.Add(entry); _isDirty = true; } @@ -98,6 +98,7 @@ public class Syncer : ISyncer where T : class, IDataModel, new() { if (dbName != _dbName) return; + if (_syncing) return; @@ -137,7 +138,13 @@ public class Syncer : ISyncer where T : class, IDataModel, new() var json = new List(); foreach (var entry in _entries) json.Add(entry.JsonData()); - File.WriteAllLines(_dbPath, json); + using (var fh = File.OpenWrite(_dbPath)) + { + foreach (var line in json) + fh.Write(Encoding.UTF8.GetBytes(line + "\n")); + fh.Flush(); + fh.Close(); + } Console.WriteLine($"Updated {_dbPath}."); _isDirty = false; _syncing = false;