Int在android中的字符串比较

时间:2017-09-22 18:40:56

标签: java android swift

您好我正试图在swift中写下面的内容。这适用于firebase和ios我可以发送消息和所有。 ios的唯一问题是当我尝试查看在android中发出的消息时,我认为这是因为时间戳。请帮忙解决这个问题。

    import Foundation
import Firebase
import UIKit

struct ChatFunctions {

    var chatRoom_id = String()
    var databaseRef: DatabaseReference!


    mutating func startChat(user1: User3, user2: User3) {

        let userId1 = user1.uid
        let userId2 = user2.uid

        var chatRoomId: String = ""

        let comparison = userId1?.compare(userId2!).rawValue

        let members = ["user1": user1.username, "user2": user2.username]

        if comparison! < 0 {
            chatRoomId = userId1! + userId2!
        } else {
            chatRoomId = userId2! + userId1!
        }

        self.chatRoom_id = chatRoomId
        self.createChatRoomId(user1: user1, user2: user2, members: members, chatRoomId: chatRoomId)

    }


    private func createChatRoomId(user1: User3, user2: User3, members: [String: String?], chatRoomId: String) {

        let chatRoomRef = Database.database().reference().child("ChatRooms").queryOrdered(byChild: "chatRoomId").queryEqual(toValue: chatRoomId)

        chatRoomRef.observe(.value , with: { (snapshot: DataSnapshot) in

            var createChatRoom = true

            if snapshot.exists() {

                if let values = snapshot.value as? [String: AnyObject] {

                    for chatRoom in values {

                        if let room = chatRoom.value["chatRoomId"] as? String  {
                            if room == chatRoomId {
                                createChatRoom = false
                            }

                        }
                    }
                }
            }

            if createChatRoom == true {
                self.createNewChatRoom(user1: user1, user2: user2, members: members, chatRoomId: chatRoomId, lastMessage: "", userProfilePic: user1.profilePic!, other_UserProfilePic: user2.profilePic!, date:NSNumber(value: Date().timeIntervalSince1970))
            }

        }) { (error) in
            alerts().errorAlert(title: "Error", message: error.localizedDescription, duration: 1)
        }

    }

    private func createNewChatRoom(user1: User3, user2: User3, members: [String: String?], chatRoomId: String, lastMessage: String, userProfilePic: String, other_UserProfilePic: String, date: NSNumber) {

        let newChatRoom = ChatRoom(username: user1.username!, other_Username: user2.username!, userId: user1.uid!, other_UserId: user2.uid!, members: members, chatRoomId: chatRoomId, lastMessage: lastMessage, userProfilePic: userProfilePic, other_UserProfilePic: other_UserProfilePic, date: date)

        let chatRoomRef = Database.database().reference().child("ChatRooms").child(chatRoomId)
        chatRoomRef.setValue(newChatRoom.toAnyObject())

    }

}

这是代码的android版本。出于某种原因,当我尝试打开android上的聊天室时,它会清除该聊天室服务器上保存的所有数据。我看起来像是在检查聊天室是否已经存在,但仍然创建它。

public class ChatFunctions implements Parcelable {

private String chatRoom_id;
private FirebaseDatabase database;

public ChatFunctions() {
}

public ChatFunctions(Parcel in) {
    readFromParcel(in);
}

public ChatFunctions(String chatRoom_id) {
    this.chatRoom_id = chatRoom_id;
}

public String getChatRoom_id() {
    return chatRoom_id;
}

public void setChatRoom_id(String chatRoom_id) {
    this.chatRoom_id = chatRoom_id;
}


public void startChat(User3 user1, User3 user2) {
    String userId1 = user1.getUid();
    String userId2 = user2.getUid();

    String chatRoomId;

    int comparison = userId1.compareTo(userId2);
    Map<String, Object> members1 = new HashMap<>();
    members1.put("user1", user1.getUsername());
    members1.put("user2", user2.getUsername());

    if (comparison < 0) {
        chatRoomId = userId1 + userId2;
    } else {
        chatRoomId = userId2 + userId1;
    }


    setChatRoom_id(chatRoomId);


    createChatRoomId(user1, user2, members1, chatRoomId);

}

private void createChatRoomId(final User3 user1, final User3 user2, final Map<String, Object> members, final String chatRoomId) {
    database = FirebaseDatabase.getInstance();

     database.getReference().child("ChatRooms").orderByChild("chatRoomId").equalTo(chatRoomId).addValueEventListener(new ValueEventListener() {

        boolean createChatRoom = true;
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            if (dataSnapshot.exists()) {
               Map<String, Object> values = (Map<String, Object>) dataSnapshot.getValue();
                for (Object chatRoom : values.values()) {
                    String room = chatRoom.toString();
                    if (room.equals(chatRoomId)) {
                        createChatRoom = false;
                    }
                }
            } else {
                createChatRoom = true;
                Log.v("Room", "true");
            }

            if (createChatRoom == true) {
                createNewChatRoom(user1, user2, members, chatRoomId, "", user1.getProfilePic(), user2.getProfilePic(), new Date().getTime() * 1000);
                Log.v("Room", "false");
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}
private void createNewChatRoom(User3 user1, User3 user2, Map<String, Object> members, String chatRoomId, String lastMessage, String userProfilePic, String other_UserProfilePic, Long date) {
    DefaultDialogModel newChatRoom = new DefaultDialogModel(user1.getUsername(), user2.getUsername(), user1.getUid(), user2.getUid(), members, chatRoomId, userProfilePic, lastMessage, other_UserProfilePic, date);
    DatabaseReference chatRoomRef = database.getReference().child("ChatRooms").child(chatRoomId);
    chatRoomRef.setValue(newChatRoom);
}

@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel parcel, int i) {
    parcel.writeString(chatRoom_id);
}

private void readFromParcel(Parcel in) {
    chatRoom_id = in.readString();
}


public static final Parcelable.Creator CREATOR = new Parcelable.ClassLoaderCreator<ChatFunctions>() {

    @Override
    public ChatFunctions createFromParcel(Parcel parcel) {
        return new ChatFunctions(parcel);
    }

    @Override
    public ChatFunctions[] newArray(int i) {
        return new ChatFunctions[0];
    }

    @Override
    public ChatFunctions createFromParcel(Parcel parcel, ClassLoader classLoader) {
        return null;
    }
};

}

1 个答案:

答案 0 :(得分:1)

Android Firebase API中的setValue方法会覆盖任何现有值。要更改值,请使用updateChildren方法[1]。

  1. https://firebase.google.com/docs/database/android/read-and-write#update_specific_fields