- Data base concurrency cause constraint failed when updating userinfo
This commit is contained in:
Linloir 2022-10-14 14:32:50 +08:00
parent 0bb71791da
commit 2a78af4885
No known key found for this signature in database
GPG Key ID: 58EEB209A0F2C366
6 changed files with 91 additions and 21 deletions

View File

@ -1,6 +1,44 @@
/* /*
* @Author : Linloir * @Author : Linloir
* @Date : 2022-10-13 14:03:56 * @Date : 2022-10-13 14:03:56
* @LastEditTime : 2022-10-13 14:03:56 * @LastEditTime : 2022-10-14 13:47:33
* @Description : * @Description :
*/ */
import 'dart:async';
import 'package:bloc/bloc.dart';
import 'package:tcp_client/chat/cubit/chat_state.dart';
import 'package:tcp_client/repositories/common_models/message.dart';
import 'package:tcp_client/repositories/local_service_repository/local_service_repository.dart';
import 'package:tcp_client/repositories/tcp_repository/models/tcp_response.dart';
import 'package:tcp_client/repositories/tcp_repository/tcp_repository.dart';
class ChatCubit extends Cubit<ChatState> {
ChatCubit({
required this.localServiceRepository,
required this.tcpRepository
}): super(ChatState.empty()) {
subscription = tcpRepository.responseStreamBroadcast.listen(_onResponse);
}
final LocalServiceRepository localServiceRepository;
final TCPRepository tcpRepository;
late final StreamSubscription subscription;
void addMessage(Message message) {
//Store locally
//Send to server
//Emit new state
}
void _onResponse(TCPResponse response) {
}
@override
Future<void> close() {
subscription.cancel();
return super.close();
}
}

View File

@ -1,6 +1,23 @@
/* /*
* @Author : Linloir * @Author : Linloir
* @Date : 2022-10-13 14:03:52 * @Date : 2022-10-13 14:03:52
* @LastEditTime : 2022-10-13 14:03:52 * @LastEditTime : 2022-10-14 13:42:46
* @Description : * @Description :
*/ */
import 'package:equatable/equatable.dart';
import 'package:tcp_client/repositories/common_models/message.dart';
enum ChatStatus { fetching, partial, full }
class ChatState extends Equatable {
final ChatStatus status;
final List<Message> chatHistory;
const ChatState({required this.chatHistory, required this.status});
static ChatState empty() => const ChatState(chatHistory: [], status: ChatStatus.fetching);
@override
List<Object> get props => [chatHistory];
}

View File

@ -0,0 +1,6 @@
/*
* @Author : Linloir
* @Date : 2022-10-14 13:49:47
* @LastEditTime : 2022-10-14 13:49:47
* @Description :
*/

View File

@ -0,0 +1,6 @@
/*
* @Author : Linloir
* @Date : 2022-10-14 13:49:28
* @LastEditTime : 2022-10-14 13:49:28
* @Description :
*/

View File

@ -1,7 +1,7 @@
/* /*
* @Author : Linloir * @Author : Linloir
* @Date : 2022-10-12 09:56:04 * @Date : 2022-10-12 09:56:04
* @LastEditTime : 2022-10-12 17:54:35 * @LastEditTime : 2022-10-14 14:24:11
* @Description : * @Description :
*/ */
@ -22,6 +22,7 @@ class InitializationCubit extends Cubit<InitializationState> {
TCPRepository? tcpRepository; TCPRepository? tcpRepository;
LocalServiceRepository? localServiceRepository; LocalServiceRepository? localServiceRepository;
Future(() async { Future(() async {
print('${(await getApplicationDocumentsDirectory()).path}/.data/database.db');
localServiceRepository = await LocalServiceRepository.create(databaseFilePath: '${(await getApplicationDocumentsDirectory()).path}/.data/database.db'); localServiceRepository = await LocalServiceRepository.create(databaseFilePath: '${(await getApplicationDocumentsDirectory()).path}/.data/database.db');
}).then((_) { }).then((_) {
emit(state.copyWith( emit(state.copyWith(

View File

@ -1,7 +1,7 @@
/* /*
* @Author : Linloir * @Author : Linloir
* @Date : 2022-10-11 10:56:02 * @Date : 2022-10-11 10:56:02
* @LastEditTime : 2022-10-12 15:35:30 * @LastEditTime : 2022-10-14 14:30:11
* @Description : Local Service Repository * @Description : Local Service Repository
*/ */
@ -231,20 +231,21 @@ class LocalServiceRepository {
Future<void> storeUserInfo({ Future<void> storeUserInfo({
required UserInfo userInfo required UserInfo userInfo
}) async { }) async {
await _database.transaction((txn) async {
//check if exist //check if exist
var queryResult = await _database.query( var queryResult = await txn.query(
'users', 'users',
where: 'userid = ?', where: 'userid = ?',
whereArgs: [userInfo.userID] whereArgs: [userInfo.userID]
); );
if(queryResult.isEmpty) { if(queryResult.isEmpty) {
_database.insert( txn.insert(
'users', 'users',
userInfo.jsonObject userInfo.jsonObject
); );
} }
else { else {
_database.update( txn.update(
'users', 'users',
userInfo.jsonObject, userInfo.jsonObject,
where: 'userid = ?', where: 'userid = ?',
@ -252,6 +253,7 @@ class LocalServiceRepository {
); );
} }
_userInfoChangeStreamController.add(userInfo); _userInfoChangeStreamController.add(userInfo);
});
} }
Future<UserInfo?> fetchUserInfoViaID({required int userid}) async { Future<UserInfo?> fetchUserInfoViaID({required int userid}) async {