Firebase createdAt-时间戳与日期

时间:2019-11-12 03:22:08

标签: swift firebase

尝试按createdAt对快照数组进行排序。由于Google从“日期”格式更改为“时间戳记”,因此出现了困难

我尝试使用Timestamp.dateValue()将时间戳转换为日期

当他们使用Date时,我使用了这个方法

 self.tableData = self.tableData.sorted(by: { 
 $0["createdAt"] as! Date > $1["createdAt"] as! Date })

我尝试将其与Timestamp一起使用,因为您无法直接在Timestamp上进行排序-我无法使其正常工作或语法不正确...

   self.tableData =  self.tableData.sorted(by: {
       $0["createdAt"] as Timestamp.dateValue() as! Date >
       $1["createdAt"] as Timestamp.dateValue() as! Date })

任何帮助将不胜感激。知道为什么Google从Date更改为Timestamp吗?这使排序更加困难。我知道您可以在请求期间进行排序,但是我需要根据数据库请求进行排序。

1 个答案:

答案 0 :(得分:1)

  

我这样做如下:-

  • 在映射数据时,将其映射为日期类型变量:-

    OBJECTS = $(SOURCES:$(SRC_PATH)/%.$(SRC_EXT)=$(BUILD_PATH)/%.o)
    DEP = $(OBJECTS:.o=.d)
    
    CPP_FLAGS = -std=c++11 -g -Wall -MD
    
    .PHONY: default_target
    default_target: release
    
    .PHONY: release
    release: dirs
        @$(MAKE) all
    
    .PHONY: dirs
    dirs:
        @echo "Creating directories"
        @mkdir -p $(dir $(OBJECTS))
        @mkdir -p $(BIN_PATH)
    
    .PHONY: clean
    clean:
        @echo "Deleting $(BIN_NAME) symlink"
        @$(RM) $(BIN_NAME)
        @echo "Deleting directories"
        @$(RM) -r $(BUILD_PATH)
        @$(RM) -r $(BIN_PATH)
        @$(RM) $(DEPS)
    
    # checks the executable and symlinks to the output
    .PHONY: all
    all: $(BIN_PATH)/$(BIN_NAME)
        @echo "Making symlink: $(BIN_NAME) -> $<"
        @$(RM) $(BIN_NAME)
        @ln -s $(BIN_PATH)/$(BIN_NAME) $(BIN_NAME)
    
    # Creation of the executable
    $(BIN_PATH)/$(BIN_NAME): $(OBJECTS)
        @echo "Linking: $@"
        $(CXX) $(CPPFLAGS) $(LFLAGS) -o $@ $(OBJECTS) $(LIBS)
    
    # Add dependency files, if they exist
    -include $(DEPS)
    
    # Source file rules
    # After the first compilation they will be joined with the rules from the
    # dependency files to provide header dependencies
    $(BUILD_PATH)/%.o: $(SRC_PATH)/%.$(SRC_EXT)
        @echo "Compiling: $< -> $@"
        $(CXX) $(CPP_FLAGS) $(LFLAGS) -c $< -o $@ $(LIBS) 
    
    
    
  • 然后我对数组进行如下排序:-

    let createdAt : Date? = snapshotValue["createdAt"] as? Date  
    

OR

您可以直接获取排序的数据,如下所示:-

let currentArr = commentsArray.sorted(by: {
        $0.createdAt!.compare($1.createdAt!) == .orderedDescending
    })