updateData method
Updates existing data
at the specified path
in the RTDB.
path
: The database path to update data at.data
: The data to update as aMap<String, dynamic>
.
Returns true
if the operation is successful, false
otherwise.
Implementation
Future<bool> updateData(String path, Map<String, dynamic> data) async {
_startRateLimitResetTimer();
final authToken = await _getAuthToken();
if (authToken == null) return false;
final url = Uri.parse('$_baseUrl$path.json?auth=$authToken');
final jsonData = json.encode(data);
final dataSize = jsonData.length;
if (!_canProceedWithRequest(dataSize, isWrite: true)) {
debugPrint('Rate limit exceeded for update operation.');
return false;
}
// Manage connection limit
if (_currentHttpConnections >= maxConnections) {
String oldestConnection = _connectionOrder.removeAt(0);
_runningStreams[oldestConnection]?.close();
_runningStreams.remove(oldestConnection);
}
try {
_currentHttpConnections++;
_connectionOrder.add(path);
final response = await _client.patch(
url,
headers: _jsonHeaders,
body: jsonData,
);
_currentHttpConnections--;
if (response.statusCode == 200) {
_currentWrites++;
_currentDataTransferred += dataSize;
debugPrint('Document updated successfully at $path.');
return true;
} else {
debugPrint('Error updating data. Status code: ${response.statusCode}');
return false;
}
} catch (e) {
_currentHttpConnections--;
debugPrint('Error updating data: $e');
return false;
}
}